My Phase2 FlatIron Project
Tags: beginners, html
Published: 2021-03-28 19:43:24

Well, it's been one crazy week but I finally did it, I created a Sinatra-based web app called "What Am I Doing Today?"!! Essentially what this app is, is a to-do list that allows a user to create an account and easily keep track of their day-to-day tasks. This project came with a lot of struggles and roadblocks, so much at times that I thought I wasn't going to be able to complete it in time. That's all over now though because I have a fully functioning Sinatra web app using CRUD!

Creating an application from scratch is not something that I would consider easy. I had a lot of moments where I had to step away from the computer and really think things through before starting to write any code. One thing that made my life so much easier while creating this was the [Corneal Gem](https://github.com/thebrianemory/corneal). This gem was made by a former FlatIron student and boy did it come in handy. Essentially what the corneal gem does is it creates the complete structure of your Sinatra app for you. All you have to do is run ``gem install corneal`` and once it is installed, run `` corneal new APP-NAME``. Once you run the commands it will populate a whole file structure for you consisting of an app folder with views, models and controllers, a gemfile, db folder, etc. Pretty much everything you would need to create a Sinatra application. This definitely helped me to get my project off the ground and not have to figure out all the code necessary to make this program run as a proper Sinatra app. I highly suggest taking a look at this!

Now, getting into the creation of all the methods and making the program function at a user-friendly level. With this app we had to use something in Sinatra called CRUD. CRUD stands for Create, Read, Update and Delete. I had to make sure that when a user creates an account that they can create a new task, read their tasks, update them and also delete them. In Sinatra we store these methods in the controllers files using get, post, patch, and delete. 

### 1. Create

First I had to create a method that allowed my users to have the functionality to create a new task. 
```
get '/tasks/new' do
        if logged_in?
          erb :'/tasks/new'
        else
            redirect '/users/login'
        end
    end

    post '/tasks' do
        @task = Task.create(name: params[:name], description: params[:description])
        current_user.tasks << @task
        redirect "/tasks"
    end
```
Using the get and post method, once a user logs in they can create a task that is specific to them only.
It will display a form for the user to input the task name and task description. After the user submits that info it will be saved to their user_id. 

### 2. Read
Second, I had to create a method that allows a user to have the functionality to read/view their tasks that they have created.
```
get '/tasks/:id' do
        if logged_in?
          @task = Task.find_by_id(params[:id])
          erb :'/tasks/show'
        else
          redirect '/users/login'
        end
    end

    get '/tasks' do
        if logged_in?
          @tasks = current_user.tasks
          erb :'/tasks/index'
        else
          redirect '/users/login'
        end
    end
```
Using these two get methods, if a user is logged in to their account, they will be able to view only their tasks that they have created. No post method is needed because get methods only give you the ability to view/read something which is exactly what we need in this situation.

### 3. Update
Next, I had to create a method that gives a user the functionality to update their existing tasks.
```
get '/tasks/:id/edit' do
        if logged_in?
          @task = Task.find(params[:id])
          if current_user.id == @task.user_id
            erb :'/tasks/edit'
        else
            redirect '/users/login'
        end
    end
    end

    patch '/tasks/:id' do
        if logged_in?
          @task = Task.find(params[:id])
          @task.update(name: params[:name], description: params[:description])
          if @task.save
            redirect "/tasks/#{@task.id}"
          else
            redirect '/tasks'
          end
        end
    end
```
Using these get and patch methods, a user can easily update a task whether they want to change the name or the description. It finds the task that the user wants to update and then gives the user the options to edit either or both of the fields. Then it will save that updated information and store it as the new updated task at that id number.

### 4. Delete
Last, I had to create a method that allows a user the functionality to delete a task.
```
get '/tasks/:id/delete' do
        if logged_in?
        @task = Task.find(params[:id])
        if current_user.id == @task.user_id
            erb :'/tasks/delete'
        else
            redirect '/users/login'
        end
    end
    end

    delete '/tasks/:id' do
        if logged_in?
          @task = Task.find_by_id(params[:id])
            if @task.destroy
                redirect '/tasks'
          else
            redirect '/users/login'
          end
        end
    end
```
Using these get and delete methods, a user is able to delete a task that they have already previously created. It finds the task by its id number and then renders to an erb form that allows the user to delete that task. 

Using CRUD it gives the user the ability to do everything necessary to pass the project requirements. In all of my CRUD methods you can see that I used the a helper method called ``logged_in?``. This helper method makes it possible that there won't be any issues regarding creating, reading, updating, or deleting any other users tasks. This is used by enabling sessions. Essentially what sessions does is, that it stores all of the specific users data in a cookie so that way it knows what data belongs to what user. 
```
configure do
    set :public_folder, 'public'
    set :views, 'app/views'
    enable :sessions
    set :session_secret, ENV['SINATRA_SECRET']
  end
```
As you can see, in my application controller is where I enabled my session as well as setting the session secret. This will ensure that a user can only deal with their own tasks and data.

Well there ya go! This week was quite the rollercoaster, but hey I was kinda expecting that after all my issues with my first ever project. All in all though, building this app taught me a lot better how things kind of flow and function in Sinatra. I'm pretty proud of myself for what I was able to create and I can't wait to continue to build upon this app and hopefully one day be able to deploy it! 

You can check out my project repo if you would like at:
(https://github.com/jessicaajosephh/what-am-i-doing-today)