api

package
v0.0.0-...-4ad5de9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoalHandler

type GoalHandler struct {
	Control *handle.GoalControl
}

GoalHandler represents a handler for managing goals. It has a Control field of type *handle.GoalControl that provides functions for handling various goal operations. Example usage for creating a goal:

func (h *GoalHandler) CreateGoal(w http.ResponseWriter, r *http.Request) {
		var req handle.CreateGoalRequest
		err := json.NewDecoder(r.Body).Decode(&req)
		// Handle error
		res, err := h.Control.CreateGoal(&req)
		// Handle error
		err = json.NewEncoder(w).Encode(res)
		// Handle error
}

Example usage for getting a goal by ID:

func (h *GoalHandler) GetGoal(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id := vars["id"]
		req := handle.GetGoalRequest{Id: id}
		res, err := h.Control.GetGoal(&req)
		// Handle error
		err = json.NewEncoder(w).Encode(res)
		// Handle error
}

Example usage for getting goals by objective:

func (h *GoalHandler) GetGoalByObjective(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		objective := vars["objective"]
		req := handle.GetGoalByObjectiveRequest{Objective: objective}
		res, err := h.Control.GetGoalByObjective(&req)
		// Handle error
		err = json.NewEncoder(w).Encode(res)
		// Handle error
}

Example usage for getting goals by planner ID:

func (h *GoalHandler) GetGoalsByPlannerIdRequest(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		plannerId := vars["planner_id"]
		req := handle.GetGoalsByPlannerIdRequest{PlannerID: plannerId}
		res, err := h.Control.GetGoalsByPlannerId(&req)
		// Handle error
		err = json.NewEncoder(w).Encode(res)
		// Handle error
}

Example usage for updating a goal:

func (h *GoalHandler) UpdateGoal(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id := vars["id"]

		var req handle.UpdateGoalRequest
		err := json.NewDecoder(r.Body).Decode(&req)
		// Handle error

		req.Id = id // Ensure the ID from the URL is used

		err = h.Control.UpdateGoal(&req)
		// Handle error

		w.WriteHeader(http.StatusOK)
}

Example usage for deleting a goal:

func (h *GoalHandler) DeleteGoal(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id := vars["id"]
		req := handle.DeleteGoalRequest{Id: id}
		err := h.Control.DeleteGoal(&req)
		// Handle error
		w.WriteHeader(http.StatusOK)
}

Example usage for listing all goals:

func (h *GoalHandler) ListGoals(w http.ResponseWriter, r *http.Request) {
		res, err := h.Control.ListGoals()
		// Handle error
		err = json.NewEncoder(w).Encode(res)
		// Handle error
}

func (*GoalHandler) CreateGoal

func (h *GoalHandler) CreateGoal(w http.ResponseWriter, r *http.Request)

CreateGoal is a method of the GoalHandler struct that handles the creation of a new goal. It takes an HTTP response writer and request as parameters. The request body is decoded into a handle.CreateGoalRequest struct. If there is an error decoding the request body, a Bad Request HTTP response is returned. The CreateGoal method of the GoalControl struct is then called with the decoded request as a parameter. If there is an error creating the goal, an Internal Server Error HTTP response is returned. The response is encoded into the response writer as JSON. If there is an error encoding the response, an Internal Server Error HTTP response is returned.

func (*GoalHandler) DeleteGoal

func (h *GoalHandler) DeleteGoal(w http.ResponseWriter, r *http.Request)

DeleteGoal deletes a goal based on the ID provided in the request URL.

Example:

handler := &GoalHandler{Control: &GoalControl{Service: goalService}}
router.HandleFunc("/goals/{id}", handler.DeleteGoal).Methods(http.MethodDelete)

// Request: DELETE /goals/123

This handler function expects a DELETE HTTP request to the "/goals/{id}" URL pattern, where "{id}" is the ID of the goal to be deleted.

The function retrieves the ID from the request URL, creates a DeleteGoalRequest with the ID, and calls the DeleteGoal method of the GoalControl struct passed in the GoalHandler.

If there is an error while executing the DeleteGoal method, the function calls the handleError function to handle the error. If no error occurs, the function writes a 200 OK status code to the response writer.

Example response body:

HTTP/1.1 200 OK

func (*GoalHandler) GetGoal

func (h *GoalHandler) GetGoal(w http.ResponseWriter, r *http.Request)

GetGoal is a method of GoalHandler that retrieves a specific goal based on the provided ID. It takes in a http.ResponseWriter `w` and http.Request `r` as parameters. The `w` is used to write the response back to the client, and `r` is used to obtain the ID of the goal from the request URL. It begins by extracting the ID from the request URL using mux.Vars(r). Then, it creates a GetGoalRequest object with the extracted ID. Next, it calls the GetGoal method of the GoalControl instance within the GoalHandler instance, passing the GetGoalRequest object as a parameter. The result of the GetGoal method is stored in `res`, and any error that occurred is stored in `err`. If an error occurred while executing the GetGoal method, it is handled by calling the handleError function, which writes an error response with the corresponding HTTP status code to the client and logs the error. If no error occurred, the result `res` is encoded as JSON and written as the response to the client. Any error encountered during encoding is also handled by calling the handleError function. Example Usage:

handler := GoalHandler{Control: &GoalControl{Service: &services.GoalService{}}}
router.HandleFunc("/goal/{id}", handler.GetGoal).Methods("GET")

HTTP GET /goal/1 -> calls GetGoal method with ID = "1"
Result -> goal object with ID = "1" is returned as response JSON

func (*GoalHandler) GetGoalByObjective

func (h *GoalHandler) GetGoalByObjective(w http.ResponseWriter, r *http.Request)

GetGoalByObjective retrieves a goal by its objective. It takes the objective as a URL parameter and sends it to the GoalControl's GetGoalByObjective method. If an error occurs while retrieving the goal or encoding the response, it returns a 500 Internal Server Error. The retrieved goal is encoded as JSON and written to the http.ResponseWriter.

Example:

GET /goals?objective=example

{
	"goal": {
		"id": "123",
		"objective": "example",
		"deadline": "2022-01-01T00:00:00Z",
		"created_at": "2021-01-01T00:00:00Z",
		"updated_at": "2021-01-01T00:00:00Z"
	}
}

func (*GoalHandler) GetGoalsByPlannerIdRequest

func (h *GoalHandler) GetGoalsByPlannerIdRequest(w http.ResponseWriter, r *http.Request)

GetGoalsByPlannerIdRequest retrieves a list of goals by planner ID from the goal control.

It takes a HTTP response writer and request as parameters.

It extracts the planner ID from the request URL using Gorilla Mux and creates a GetGoalsByPlannerIdRequest object. It then calls the GetGoalsByPlannerId method of the goal control to retrieve the goals. Any errors that occur during the process are handled using the handleError function. Finally, the retrieved goals are encoded as JSON and written to the response.

Example usage:

handler := &GoalHandler{
    Control: &handle.GoalControl{
        Service: &services.GoalService{},
    },
}
http.HandleFunc("/goals/{planner_id}", handler.GetGoalsByPlannerIdRequest)
log.Fatal(http.ListenAndServe(":8080", nil))

func (*GoalHandler) ListGoals

func (h *GoalHandler) ListGoals(w http.ResponseWriter, r *http.Request)

ListGoals retrieves a list of goals.

It calls the ListGoals method of GoalControl and encodes the response as JSON. If there is an error retrieving the goals, it returns an HTTP 500 Internal Server Error.

Example usage:

goalHandler := &GoalHandler{Control: goalControl}
http.HandleFunc("/goals", goalHandler.ListGoals)

func (*GoalHandler) UpdateGoal

func (h *GoalHandler) UpdateGoal(w http.ResponseWriter, r *http.Request)

UpdateGoal updates a goal with the provided ID based on the request body. It first decodes the request body into an update goal request object. Then it ensures that the ID from the URL is used. It then calls the UpdateGoal method of the GoalControl service. If there is an error during the update, it returns a 500 Internal Server Error response. If the update is successful, it returns a 200 OK response. If there is an error decoding the request body, it returns a 400 Bad Request response.

Example usage:

req := handle.UpdateGoalRequest{
    Id:        "goalId",
    Objective: "New objective",
    Deadline:  "2022-01-01",
    PlannerID: "plannerId",
}
h.Control.UpdateGoal(&req)

type PlanHandler

type PlanHandler struct {
	Control *handle.PlanControl
}

PlanHandler is a type that handles HTTP requests related to plans. It contains a control object of type *handle.PlanControl, which is used to interact with the plan service.

CreatePlan handles the creation of a new plan. It decodes the request body into a CreatePlanRequest, calls the CreatePlan method on PlanControl, and encodes the response in the HTTP response writer.

UpdatePlan handles the updating of an existing plan. It decodes the request body into an UpdatePlanRequest, calls the UpdatePlan method on PlanControl, and returns any errors that occur during the process.

DeletePlan handles the deletion of a plan. It extracts the plan ID from the request URL, creates a DeletePlanRequest with the ID, calls the DeletePlan method on PlanControl, and returns any errors that occur during the process.

GetPlan handles the retrieval of a plan by ID. It extracts the plan ID from the request URL, creates a GetPlanRequest with the ID, calls the GetPlan method on PlanControl, and returns the retrieved plan or any errors that occur during the process.

GetPlanByName handles the retrieval of a plan by name. It extracts the plan name from the request URL, creates a GetPlanByNameRequest with the name, calls the GetPlanByName method on PlanControl, and returns the retrieved plan or any errors that occur during the process.

GetPlansByGoal handles the retrieval of all plans associated with a goal. It extracts the goal ID from the request URL, creates a GetPlansByGoalRequest with the ID, calls the GetPlansByGoal method on PlanControl, and returns the retrieved plans or any errors that occur during the process.

ListPlans handles the listing of all plans. It calls the ListPlans method on PlanControl and returns the retrieved plans or any errors that occur during the process.

func (*PlanHandler) CreatePlan

func (h *PlanHandler) CreatePlan(w http.ResponseWriter, r *http.Request)

CreatePlan takes an HTTP response writer and request as input. It decodes the request body into a CreatePlanRequest object. If any error occurs during the decoding, it is handled by the handleError function. It then sends the CreatePlanRequest to the PlanControl's CreatePlan method to create a new plan. If any error occurs during the creation process, it is handled by the handleError function. Finally, it encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the encoding process, it is handled by the handleError function.

Example usage: var h PlanHandler h.CreatePlan(w, r)

func (*PlanHandler) DeletePlan

func (h *PlanHandler) DeletePlan(w http.ResponseWriter, r *http.Request)

DeletePlan deletes a plan by its ID

func (*PlanHandler) GetPlan

func (h *PlanHandler) GetPlan(w http.ResponseWriter, r *http.Request)

GetPlan takes an HTTP response writer and request as input. It retrieves the "id" parameter from the URL, creates a GetPlanRequest, and sends it to the PlanControl's GetPlan method to retrieve the plan information. The function then encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the process, it is handled by the handleError function.

func (*PlanHandler) GetPlanByName

func (h *PlanHandler) GetPlanByName(w http.ResponseWriter, r *http.Request)

GetPlanByName gets a plan by its name

func (*PlanHandler) GetPlansByGoal

func (h *PlanHandler) GetPlansByGoal(w http.ResponseWriter, r *http.Request)

GetPlansByGoal retrieves plans based on a goal ID It takes in a response writer, a request object, and extracts the goal ID from the request URL It creates a GetPlansByGoalRequest with the extracted goal ID It then calls the GetPlansByGoal method on the PlanControl instance, passing in the request object Any error encountered during the process is handled using the handleError function The response is encoded into JSON format and sent in the response writer Any error encountered during the encoding process is handled using the handleError function

func (*PlanHandler) ListPlans

func (h *PlanHandler) ListPlans(w http.ResponseWriter, r *http.Request)

ListPlans fetches a list of plans and encodes them as JSON before returning the response to the HTTP client

func (*PlanHandler) UpdatePlan

func (h *PlanHandler) UpdatePlan(w http.ResponseWriter, r *http.Request)

UpdatePlan updates an existing plan

type PlannerHandler

type PlannerHandler struct {
	Control *handle.PlannerControl
}

PlannerHandler is a type that handles planner-related operations. It contains a reference to the PlannerControl that provides the implementation for these operations. It has methods to handle the following HTTP requests: - CreatePlanner: handles the creation of a new planner. - GetPlanner: handles the retrieval of a planner by ID. - GetPlannerByTitle: handles the retrieval of a planner by title. - GetPlannerByOwner: handles the retrieval of planners by owner. - UpdatePlanner: handles the updating of an existing planner. - DeletePlanner: handles the deletion of a planner.

func (*PlannerHandler) CreatePlanner

func (h *PlannerHandler) CreatePlanner(w http.ResponseWriter, r *http.Request)

CreatePlanner takes an HTTP response writer and request as input. It decodes the request body into a CreatePlannerRequest object. If any error occurs during the decoding, it is handled by the handleError function. It then sends the CreatePlannerRequest to the PlannerControl's CreatePlanner method to create a new planner. If any error occurs during the creation process, it is handled by the handleError function. Finally, it encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the encoding process, it is handled by the handleError function.

func (*PlannerHandler) DeletePlanner

func (h *PlannerHandler) DeletePlanner(w http.ResponseWriter, r *http.Request)

DeletePlanner takes an HTTP response writer and request as input. It sets the "Content-Type" header of the response to "application/json". It gets the "id" parameter from the request URL using the mux.Vars() function. It creates a DeletePlannerRequest object with the extracted id and passes it to h.Control.DeletePlanner(). If any error occurs during the deletion process, it is handled by the handleError function. It sets the HTTP response writer status code to 200 (OK).

func (*PlannerHandler) GetPlanner

func (h *PlannerHandler) GetPlanner(w http.ResponseWriter, r *http.Request)

GetPlanner takes an HTTP response writer and request as input. It sets the "Content-Type" header of the response to "application/json". It extracts the "id" variable from the request's route parameters. It creates a GetPlannerRequest with the extracted id. It sends the GetPlannerRequest to the PlannerControl's GetPlanner method to retrieve the planner. If any error occurs during the retrieval process, it is handled by the handleError function. It encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the encoding process, it is handled by the handleError function.

func (*PlannerHandler) GetPlannerByOwner

func (h *PlannerHandler) GetPlannerByOwner(w http.ResponseWriter, r *http.Request)

GetPlannerByOwner takes an HTTP response writer and request as input. It sets the "Content-Type" header to "application/json". It retrieves the "owner" variable from the request URL parameters. It creates a GetPlannerByOwnerRequest object with the UserId field set to the value of "owner". It sends the GetPlannerByOwnerRequest to the PlannerControl's GetPlannerByOwner method to retrieve the planner by owner. If any error occurs during the retrieval process, it is handled by the handleError function. Finally, it encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the encoding process, it is handled by the handleError function.

func (*PlannerHandler) GetPlannerByTitle

func (h *PlannerHandler) GetPlannerByTitle(w http.ResponseWriter, r *http.Request)

GetPlannerByTitle takes an HTTP response writer and request as input. It extracts the "title" variable from the request URL path and assigns it to the "title" variable. It then creates a GetPlannerByTitleRequest object with the extracted title. It sends the GetPlannerByTitleRequest to the PlannerControl's GetPlannerByTitle method to retrieve the planner with the given title. If any error occurs during the retrieval process, it is handled by the handleError function. Finally, it encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the encoding process, it is handled by the handleError function.

func (*PlannerHandler) ListPlanners

func (h *PlannerHandler) ListPlanners(w http.ResponseWriter, r *http.Request)

ListPlanners takes an HTTP response writer and request as input. It sets the "Content-Type" header of the response to "application/json". It then calls the ListPlanners method of the PlannerControl to retrieve a list of planners. If any error occurs during the retrieval process, it is handled by the handleError function. Finally, it encodes the response using JSON and writes it to the HTTP response writer. If any error occurs during the encoding process, it is handled by the handleError function.

func (*PlannerHandler) UpdatePlanner

func (h *PlannerHandler) UpdatePlanner(w http.ResponseWriter, r *http.Request)

UpdatePlanner takes an HTTP response writer and request as input. It sets the content type header of the response writer to application/json. It gets the "id" parameter from the request URL using mux.Vars. It creates an UpdatePlannerRequest object with the "id" parameter set. It decodes the request body into the UpdatePlannerRequest object. If any error occurs during the decoding, it is handled by the handleError function. It sends the UpdatePlannerRequest to the PlannerControl's UpdatePlanner method to update the planner. If any error occurs during the update process, it is handled by the handleError function. It sets the HTTP status code to 200 (OK).

type TaskHandler

type TaskHandler struct {
	Control *handle.TaskControl
}

TaskHandler handles HTTP requests related to tasks. It contains a control object of type *handle.TaskControl, which is used to interact with the task service. It has methods to handle the following HTTP requests: - CreateTask: handles the creation of a new task. - UpdateTask: handles the updating of an existing task. - DeleteTask: handles the deletion of a task. - GetTask: handles the retrieval of a task by ID. - GetTaskByTitle: handles the retrieval of a task by title. - GetTaskByOwner: handles the retrieval of tasks by owner. - ListTasks: handles the listing of all tasks.

func (*TaskHandler) CreateTask

func (h *TaskHandler) CreateTask(w http.ResponseWriter, r *http.Request)

CreateTask creates a new task based on the request data. It decodes the JSON request body to a CreateTaskRequest struct. Then, it generates a unique ID for the task, creates a task instance using the provided data, and calls the CreateTask method of the TaskControl. If the task creation is successful, it returns a CreateTaskResponse with the ID of the created task. If any error occurs during the process, it handles the error by writing an HTTP error response with the corresponding status code and logging the error message. Finally, it encodes the response data into JSON and writes it to the HTTP response.

func (*TaskHandler) DeleteTask

func (h *TaskHandler) DeleteTask(w http.ResponseWriter, r *http.Request)

DeleteTask deletes a task with the given ID. It extracts the task ID from the URL parameters and creates a DeleteTaskRequest with that ID. Then, it calls the DeleteTask method of TaskControl to delete the task. If there is any error during task deletion, it returns an Internal Server Error status. Finally, it sets the response writer's status code to OK.

func (*TaskHandler) GetTask

func (h *TaskHandler) GetTask(w http.ResponseWriter, r *http.Request)

GetTask retrieves a task by its ID.

func (*TaskHandler) GetTaskByOwner

func (h *TaskHandler) GetTaskByOwner(w http.ResponseWriter, r *http.Request)

GetTaskByOwner is a method of TaskHandler that handles the GET request to retrieve tasks by owner. It expects the request to include an "owner" path variable. It retrieves tasks based on the owner from the request and returns them as a JSON response. If there is an error during the process, it returns a JSON error response with the corresponding status code.

func (*TaskHandler) GetTaskByTitle

func (h *TaskHandler) GetTaskByTitle(w http.ResponseWriter, r *http.Request)

GetTaskByTitle is a method of TaskHandler that handles the GET request to retrieve a task by its title. It expects the request URL to contain a "title" parameter. It creates a GetTaskByTitleRequest with the title parameter and passes it to the TaskControl's GetTaskByTitle method. If there is an error during the process, it returns a JSON error response with the corresponding status code. It encodes the response into JSON format and writes it to the ResponseWriter. If there is an error encoding the response, it returns a JSON error response with the corresponding status code.

func (*TaskHandler) ListTasks

func (h *TaskHandler) ListTasks(w http.ResponseWriter, r *http.Request)

ListTasks retrieves a list of tasks. It calls the ListTasks method of the TaskControl to retrieve the tasks. If there is any error during the retrieval, it returns an Internal Server Error status. Finally, it encodes the retrieved tasks into JSON and writes it to the response writer. If there is any error during encoding, it returns an Internal Server Error status.

func (*TaskHandler) UpdateTask

func (h *TaskHandler) UpdateTask(w http.ResponseWriter, r *http.Request)

UpdateTask is a method of TaskHandler that handles the PUT request to update a specific task. It expects the request to include an ID path variable and a request body containing the updated task information. The ID from the URL is used to ensure the correct task is updated. If there is an error during the process, it returns a JSON error response with the corresponding status code. If the update is successful, it returns a 200 OK status code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL