57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
|
package taskcontroller
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors"
|
||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
||
|
"github.com/go-chi/render"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|