aides-repo-api/internal/controllers/taskcontroller/create.go
Maxim Slipenko e4f9743dea
Some checks failed
Format and Lint / format-check (push) Failing after 2s
Format and Lint / test (push) Successful in 52s
chore: change code.alt-gnome to code.aides.space
2025-01-07 17:54:16 +03:00

70 lines
1.7 KiB
Go

package taskcontroller
import (
"encoding/json"
"net/http"
"github.com/go-chi/render"
"code.aides.space/aides-infra/aides-repo-api/internal/common/errors"
"code.aides.space/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
// @Security ApiKeyAuth
// @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
}
}