package agenda import ( "errors" "nixcn-cms/internal/exception" "nixcn-cms/service/service_agenda" "nixcn-cms/tracer" "nixcn-cms/utils" "github.com/gin-gonic/gin" "github.com/google/uuid" ) // Update handles editing an agenda item's name or description. // // @Summary Update Agenda // @Description Submitter may edit their own pending agendas before the event deadline. Managers may edit any agenda with no restrictions. // @Tags Agenda // @Accept json // @Produce json // @Security Bearer // @Param body body service_agenda.AgendaUpdateData true "Agenda Update Data" // @Success 200 {object} utils.RespStatus{data=nil} // @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input / Not Pending / Deadline Passed" // @Failure 403 {object} utils.RespStatus{data=nil} "Not Submitter" // @Failure 404 {object} utils.RespStatus{data=nil} "Agenda Not Found" // @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error" // @Router /agenda/update [patch] func (self *AgendaHandler) Update(c *gin.Context) { ctx, span := tracer.StartSpan( c.Request.Context(), "api_agenda", "update", ) defer span.End() ctx = exception.ContextWithEndpoint(ctx, exception.EndpointAgendaUpdate) ctx = exception.ContextWithService(ctx, exception.ServiceEndpoint) userIdOrig, ok := c.Get("user_id") if !ok { errorCode := exception.New( exception.WithStatus(exception.StatusUser), exception.WithType(exception.TypeCommon), exception.WithOriginal(exception.CommonErrorMissingUserId), exception.WithError(errors.New("Missing UserId")), ).Throw(ctx).String() utils.HttpResponse(c, 403, errorCode) return } userId, err := uuid.Parse(userIdOrig.(string)) if err != nil { errorCode := exception.New( exception.WithStatus(exception.StatusServer), exception.WithType(exception.TypeCommon), exception.WithOriginal(exception.CommonErrorUuidParseFailed), exception.WithError(err), ).Throw(ctx).String() utils.HttpResponse(c, 500, errorCode) return } permissionLevelOrig, ok := c.Get("permission_level") if !ok { errorCode := exception.New( exception.WithStatus(exception.StatusUser), exception.WithType(exception.TypeCommon), exception.WithOriginal(exception.CommonErrorPermissionDenied), exception.WithError(errors.New("Missing PermissionLevel")), ).Throw(ctx).String() utils.HttpResponse(c, 403, errorCode) return } data := new(service_agenda.AgendaUpdateData) if err := c.ShouldBindJSON(data); err != nil { errorCode := exception.New( exception.WithStatus(exception.StatusUser), exception.WithType(exception.TypeCommon), exception.WithOriginal(exception.CommonErrorInvalidInput), exception.WithError(err), ).Throw(ctx).String() utils.HttpResponse(c, 400, errorCode) return } if data.AgendaId == uuid.Nil { errorCode := exception.New( exception.WithStatus(exception.StatusUser), exception.WithType(exception.TypeCommon), exception.WithOriginal(exception.CommonErrorInvalidInput), exception.WithError(errors.New("agenda_id is required")), ).Throw(ctx).String() utils.HttpResponse(c, 400, errorCode) return } data.PermissionLevel = permissionLevelOrig.(uint) result := self.svc.Update(&service_agenda.AgendaUpdatePayload{ Context: ctx, UserId: userId, Data: data, }) if result.Common.Exception.Original != exception.CommonSuccess { utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String()) return } utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String()) }