aides-repo-api/internal/controllers/taskcontroller/create.go
Maxim Slipenko db49ea2c51
All checks were successful
Format and Lint / format-check (push) Successful in 14m34s
chore: wip
2024-12-17 19:28:37 +03:00

69 lines
1.7 KiB
Go

package taskcontroller
import (
"encoding/json"
"net/http"
"github.com/go-chi/render"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
)
type CreateTaskDTO struct {
Repo string
}
type CreateTaskResponse struct {
TaskID uint `json:"taskID"`
Status models.TaskStatus `json:"status"`
}
func (c *CreateTaskResponse) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
// Create a new task
//
// @Summary Create a new task
// @Description Create a new task for a specific repository
// @Tags tasks
// @Accept json
// @Produce json
// @Param body body CreateTaskDTO true "Request body to create a task"
// @Success 201 {object} CreateTaskResponse
// @Failure 400 {object} errors.ErrResponse "Invalid JSON or missing required fields"
// @Failure 500 {object} errors.ErrResponse "Internal server error"
// @Router /tasks [post]
func (c *TaskController) Create(w http.ResponseWriter, r *http.Request) {
createTaskDto := CreateTaskDTO{}
if err := json.NewDecoder(r.Body).Decode(&createTaskDto); err != nil {
render.Render(w, r, &errors.ErrResponse{
HTTPStatusCode: http.StatusBadRequest,
StatusText: "Invalid JSON",
Err: err,
})
return
}
task, err := c.taskService.Create(createTaskDto.Repo)
if err != nil {
render.Render(w, r, &errors.ErrResponse{
HTTPStatusCode: http.StatusInternalServerError,
StatusText: "Internal Server Error",
Err: err,
})
return
}
response := CreateTaskResponse{
TaskID: task.ID,
Status: task.Status,
}
if err := render.Render(w, r, &response); err != nil {
render.Render(w, r, errors.ErrRender(err))
return
}
}