-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgin_endpoints.go
More file actions
94 lines (72 loc) · 2.99 KB
/
gin_endpoints.go
File metadata and controls
94 lines (72 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"net/http"
"github.com/Stogas/feedback-api/internal/dto"
"github.com/Stogas/feedback-api/internal/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}
func GetIssuesEndpoint(c *gin.Context) {
logger := getLogger(c.Request.Context())
db := c.MustGet("db").(*gorm.DB)
var issues []models.Issue
if err := db.Find(&issues).Error; err != nil {
logger.Error("Failed to fetch issue types from DB")
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Database read error"})
}
c.JSON(http.StatusOK, dto.MapIssuesToIssueResponses(issues))
}
func submitReportEndpoint(c *gin.Context) {
newReport := c.MustGet("report").(models.Report)
logger := getLogger(c.Request.Context())
db := c.MustGet("db").(*gorm.DB)
var existingReport models.Report
existingRow := db.Where("uuid = ?", newReport.UUID).First(&existingReport)
if existingRow.Error == nil {
logger.Warn("A submission with this UUID already exists", "uuid", newReport.UUID, "method", c.Request.Method)
c.JSON(http.StatusConflict, gin.H{"error": "A submission with this UUID already exists", "uuid": newReport.UUID, "created_at": existingReport.CreatedAt})
return
} else if existingRow.Error != gorm.ErrRecordNotFound {
logger.Error("Error reading database", "error", existingRow.Error, "uuid", newReport.UUID)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database read error"})
return
}
result := db.Create(&newReport)
if result.Error != nil {
logger.Error("Database write error", "error", result.Error)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database write error"})
return
}
c.JSON(http.StatusCreated, dto.MapReportToReportResponse(newReport))
}
func updateReportEndpoint(c *gin.Context) {
newReport := c.MustGet("report").(models.Report)
logger := getLogger(c.Request.Context())
db := c.MustGet("db").(*gorm.DB)
var existingReport models.Report
existingRow := db.Where("uuid = ?", newReport.UUID).First(&existingReport)
if existingRow.Error == gorm.ErrRecordNotFound {
logger.Warn("A PATCH submission tried to modify a non-existing resource", "uuid", newReport.UUID, "method", c.Request.Method)
c.JSON(http.StatusNotFound, gin.H{"error": "A submission with this UUID has not been found, submit via HTTP POST instead", "uuid": newReport.UUID})
return
} else if existingRow.Error != nil {
logger.Error("Error reading database", "error", existingRow.Error, "uuid", newReport.UUID)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database read error"})
return
}
newReport.ID = existingReport.ID
newReport.CreatedAt = existingReport.CreatedAt
newReport.DeletedAt = existingReport.DeletedAt
result := db.Save(&newReport)
if result.Error != nil {
logger.Error("Database write error", "error", result.Error)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database write error"})
return
}
c.JSON(http.StatusOK, dto.MapReportToReportResponse(newReport))
}