| | package controllers |
| |
|
| | import ( |
| | "encoding/json" |
| | "net/http" |
| | "strconv" |
| |
|
| | "dinacom-11.0-backend/models/dto" |
| | "dinacom-11.0-backend/services" |
| | "dinacom-11.0-backend/utils" |
| |
|
| | "github.com/gin-gonic/gin" |
| | "github.com/google/uuid" |
| | ) |
| |
|
| | type ReportController interface { |
| | CreateReport(ctx *gin.Context) |
| | GetReports(ctx *gin.Context) |
| | AssignWorker(ctx *gin.Context) |
| | GetAssignedReports(ctx *gin.Context) |
| | FinishReport(ctx *gin.Context) |
| | GetUserReports(ctx *gin.Context) |
| | GetWorkerAssignedReports(ctx *gin.Context) |
| | GetWorkerHistory(ctx *gin.Context) |
| | VerifyReport(ctx *gin.Context) |
| | } |
| |
|
| | type reportController struct { |
| | reportService services.ReportService |
| | } |
| |
|
| | func NewReportController(reportService services.ReportService) ReportController { |
| | return &reportController{reportService: reportService} |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) CreateReport(ctx *gin.Context) { |
| | userIDVal, exists := ctx.Get("user_id") |
| | if !exists { |
| | utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized") |
| | return |
| | } |
| | userID := userIDVal.(uuid.UUID) |
| |
|
| | file, header, err := ctx.Request.FormFile("files") |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, "Image file is required") |
| | return |
| | } |
| | defer file.Close() |
| |
|
| | jsonData := ctx.PostForm("json") |
| | if jsonData == "" { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, "JSON data is required") |
| | return |
| | } |
| |
|
| | var req dto.ReportRequest |
| | if err := json.Unmarshal([]byte(jsonData), &req); err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, "Invalid JSON format") |
| | return |
| | } |
| |
|
| | response, err := c.reportService.CreateReport(userID, file, header, req) |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Report created successfully", response) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) GetReports(ctx *gin.Context) { |
| | reports, err := c.reportService.GetReports() |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Reports retrieved", reports) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) AssignWorker(ctx *gin.Context) { |
| | var req dto.AssignWorkerRequest |
| | if err := ctx.ShouldBindJSON(&req); err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error()) |
| | return |
| | } |
| |
|
| | message, err := c.reportService.AssignWorker(req) |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, message, nil) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) GetAssignedReports(ctx *gin.Context) { |
| | reports, err := c.reportService.GetAssignedReports() |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Assigned reports retrieved", reports) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) FinishReport(ctx *gin.Context) { |
| | workerIDVal, exists := ctx.Get("user_id") |
| | if !exists { |
| | utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized") |
| | return |
| | } |
| | workerID := workerIDVal.(uuid.UUID) |
| |
|
| | file, header, err := ctx.Request.FormFile("files") |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, "Image file is required") |
| | return |
| | } |
| | defer file.Close() |
| |
|
| | jsonData := ctx.PostForm("json") |
| | if jsonData == "" { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, "JSON data is required") |
| | return |
| | } |
| |
|
| | var req dto.WorkerReportRequest |
| | if err := json.Unmarshal([]byte(jsonData), &req); err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, "Invalid JSON format") |
| | return |
| | } |
| |
|
| | if err := c.reportService.FinishReport(workerID, file, header, req.ReportID); err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Report finished successfully", nil) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) GetUserReports(ctx *gin.Context) { |
| | userIDVal, exists := ctx.Get("user_id") |
| | if !exists { |
| | utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized") |
| | return |
| | } |
| | userID := userIDVal.(uuid.UUID) |
| |
|
| | page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) |
| | limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) |
| | if page < 1 { |
| | page = 1 |
| | } |
| | if limit < 1 || limit > 100 { |
| | limit = 10 |
| | } |
| |
|
| | response, err := c.reportService.GetUserReports(userID, page, limit) |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "User reports retrieved", response) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) GetWorkerAssignedReports(ctx *gin.Context) { |
| | workerIDVal, exists := ctx.Get("user_id") |
| | if !exists { |
| | utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized") |
| | return |
| | } |
| | workerID := workerIDVal.(uuid.UUID) |
| |
|
| | page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) |
| | limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) |
| | if page < 1 { |
| | page = 1 |
| | } |
| | if limit < 1 || limit > 100 { |
| | limit = 10 |
| | } |
| |
|
| | response, err := c.reportService.GetWorkerAssignedReports(workerID, page, limit) |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Worker assigned reports retrieved", response) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) GetWorkerHistory(ctx *gin.Context) { |
| | workerIDVal, exists := ctx.Get("user_id") |
| | if !exists { |
| | utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized") |
| | return |
| | } |
| | workerID := workerIDVal.(uuid.UUID) |
| |
|
| | page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) |
| | limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) |
| | verifyAdmin := ctx.DefaultQuery("verify_admin", "false") == "true" |
| | if page < 1 { |
| | page = 1 |
| | } |
| | if limit < 1 || limit > 100 { |
| | limit = 10 |
| | } |
| |
|
| | response, err := c.reportService.GetWorkerHistory(workerID, verifyAdmin, page, limit) |
| | if err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Worker history retrieved", response) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (c *reportController) VerifyReport(ctx *gin.Context) { |
| | var req dto.VerifyReportRequest |
| | if err := ctx.ShouldBindJSON(&req); err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error()) |
| | return |
| | } |
| |
|
| | if err := c.reportService.VerifyReport(req.ReportID); err != nil { |
| | utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error()) |
| | return |
| | } |
| |
|
| | utils.SendSuccessResponse(ctx, "Report verified successfully", nil) |
| | } |
| |
|