github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/router/task.go (about)

     1  package router
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  
     7  	"github.com/gin-gonic/gin"
     8  
     9  	"github.com/ars0915/glossika-exercise/entity"
    10  	"github.com/ars0915/glossika-exercise/util/cGin"
    11  )
    12  
    13  func (rH *HttpHandler) ListTasksHandler(c *gin.Context) {
    14  	ctx := cGin.NewContext(c)
    15  
    16  	page := ctx.GetPaginator()
    17  	param := entity.ListTaskParam{
    18  		Offset: &page.Offset,
    19  		Limit:  &page.Limit,
    20  	}
    21  
    22  	data, count, err := rH.h.ListTasks(ctx, param)
    23  	if err != nil {
    24  		ctx.WithError(err).Response(http.StatusInternalServerError, "List Tasks Failed")
    25  		return
    26  	}
    27  	page.SetTotalCount(int(count))
    28  
    29  	ctx.WithPaginator(page).WithData(data).Response(http.StatusOK, "")
    30  }
    31  
    32  type createTaskBody struct {
    33  	Name   string `json:"name" binding:"required,gt=0,lte=255"`
    34  	Status uint8  `json:"status" binding:"required,oneof=0 1"`
    35  }
    36  
    37  func (rH *HttpHandler) CreateTaskHandler(c *gin.Context) {
    38  	ctx := cGin.NewContext(c)
    39  
    40  	var body createTaskBody
    41  	if err := ctx.ShouldBindJSON(&body); err != nil {
    42  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid Json")
    43  		return
    44  	}
    45  
    46  	task := entity.Task{
    47  		Name:   &body.Name,
    48  		Status: &body.Status,
    49  	}
    50  
    51  	data, err := rH.h.CreateTask(ctx, task)
    52  	if err != nil {
    53  		ctx.WithError(err).Response(http.StatusInternalServerError, "Create Task Failed")
    54  		return
    55  	}
    56  
    57  	ctx.WithData(data).Response(http.StatusOK, "")
    58  }
    59  
    60  type updateTaskBody struct {
    61  	Name   string `json:"name" binding:"required,gt=0,lte=255"`
    62  	Status uint8  `json:"status" binding:"required,oneof=0 1"`
    63  }
    64  
    65  func (rH *HttpHandler) UpdateTaskHandler(c *gin.Context) {
    66  	ctx := cGin.NewContext(c)
    67  
    68  	idStr := ctx.Param("taskID")
    69  	id, err := strconv.Atoi(idStr)
    70  	if err != nil {
    71  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid ID")
    72  		return
    73  	}
    74  
    75  	var body updateTaskBody
    76  	if err = ctx.ShouldBindJSON(&body); err != nil {
    77  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid Json")
    78  		return
    79  	}
    80  
    81  	data, err := rH.h.UpdateTask(ctx, uint(id), entity.Task{
    82  		Name:   &body.Name,
    83  		Status: &body.Status,
    84  	})
    85  	if err != nil {
    86  		ctx.WithError(err).Response(http.StatusInternalServerError, "Update Task Failed")
    87  		return
    88  	}
    89  
    90  	ctx.WithData(data).Response(http.StatusOK, "")
    91  }
    92  
    93  func (rH *HttpHandler) DeleteTaskHandler(c *gin.Context) {
    94  	ctx := cGin.NewContext(c)
    95  
    96  	idStr := ctx.Param("taskID")
    97  	id, err := strconv.Atoi(idStr)
    98  	if err != nil {
    99  		ctx.WithError(err).Response(http.StatusBadRequest, "Invalid ID")
   100  		return
   101  	}
   102  
   103  	if err := rH.h.DeleteTask(ctx, uint(id)); err != nil {
   104  		ctx.WithError(err).Response(http.StatusInternalServerError, "Delete Task Failed")
   105  		return
   106  	}
   107  
   108  	ctx.Response(http.StatusOK, "")
   109  }