aides-repo-api/internal/controllers/taskcontroller/create.go

71 lines
1.7 KiB
Go
Raw Permalink Normal View History

package taskcontroller
import (
"encoding/json"
"net/http"
2024-12-15 06:36:33 +00:00
"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
}
2024-12-15 08:39:18 +00:00
// Create a new task
//
// @Summary Create a new task
// @Description Create a new task for a specific repository
2024-12-17 16:28:37 +00:00
// @Tags tasks
2024-12-26 13:42:54 +00:00
// @Security ApiKeyAuth
2024-12-15 08:39:18 +00:00
// @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
}
}