model with generics done

This commit is contained in:
Sense T
2024-04-09 21:16:19 +08:00
parent 7dd3af3707
commit 9752e7d9ae
10 changed files with 109 additions and 91 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/gin-gonic/gin"
)
func (s *Server) getDomains(c *gin.Context) {
func getDomains(c *gin.Context) {
domains, err := controllers.GetDomains("")
if err != nil {
errorHandler(c, err)
@@ -21,7 +21,7 @@ func (s *Server) getDomains(c *gin.Context) {
})
}
func (s *Server) createDomain(c *gin.Context) {
func createDomain(c *gin.Context) {
domain := &models.Domain{}
if err := c.BindJSON(domain); err != nil {
@@ -44,7 +44,7 @@ func (s *Server) createDomain(c *gin.Context) {
})
}
func (s *Server) updateDomain(c *gin.Context) {
func updateDomain(c *gin.Context) {
domain := &models.Domain{}
if err := c.BindJSON(domain); err != nil {
@@ -65,7 +65,7 @@ func (s *Server) updateDomain(c *gin.Context) {
})
}
func (s *Server) deleteDomain(c *gin.Context) {
func deleteDomain(c *gin.Context) {
id := c.Param("id")
if err := controllers.DeleteDomain(id); err != nil {
errorHandler(c, err)

View File

@@ -9,8 +9,8 @@ import (
"github.com/gin-gonic/gin"
)
func (s *Server) getRecords(c *gin.Context) {
query := models.Record{}
func getRecords(c *gin.Context) {
query := models.Record[models.RecordContentDefault]{}
if err := c.BindQuery(&query); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
@@ -21,7 +21,7 @@ func (s *Server) getRecords(c *gin.Context) {
domain := c.Param("domain")
query.Zone = fmt.Sprintf("%s.", domain)
records, err := controllers.GetRecords(query)
records, err := controllers.GetRecords(&query)
if err != nil {
errorHandler(c, err)
return
@@ -33,8 +33,8 @@ func (s *Server) getRecords(c *gin.Context) {
})
}
func (s *Server) createRecord(c *gin.Context) {
record := &models.Record{}
func createRecord(c *gin.Context) {
record := &models.Record[models.RecordContentDefault]{}
if err := c.BindJSON(record); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
@@ -44,7 +44,7 @@ func (s *Server) createRecord(c *gin.Context) {
}
domain := c.Param("domain")
if domain != record.Zone {
if domain != record.WithOutDotTail() {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
Message: "request body doesn't match URI",
@@ -52,7 +52,7 @@ func (s *Server) createRecord(c *gin.Context) {
return
}
record, err := controllers.CreateRecord(record)
irecord, err := controllers.CreateRecord(record)
if err != nil {
errorHandler(c, err)
return
@@ -60,12 +60,12 @@ func (s *Server) createRecord(c *gin.Context) {
c.JSON(http.StatusCreated, Response{
Succeed: true,
Data: record,
Data: irecord,
})
}
func (s *Server) createRecords(c *gin.Context) {
var records []*models.Record
func createRecords(c *gin.Context) {
var records []models.IRecord
if err := c.BindJSON(&records); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
@@ -84,8 +84,8 @@ func (s *Server) createRecords(c *gin.Context) {
})
}
func (s *Server) updateRecord(c *gin.Context) {
record := &models.Record{}
func updateRecord(c *gin.Context) {
record := &models.Record[models.RecordContentDefault]{}
if err := c.BindJSON(record); err != nil {
c.JSON(http.StatusBadRequest, Response{
Succeed: false,
@@ -113,7 +113,7 @@ func (s *Server) updateRecord(c *gin.Context) {
})
}
func (s *Server) deleteRecord(c *gin.Context) {
func deleteRecord(c *gin.Context) {
domain := c.Param("domain")
id := c.Param("id")

View File

@@ -37,18 +37,18 @@ func (s *Server) setupRoute() {
domains := groupV1.Group("/domains")
domains.
GET("/", s.getDomains).
POST("/", s.createDomain).
PUT("/", s.updateDomain).
DELETE("/:id", s.deleteDomain)
GET("/", getDomains).
POST("/", createDomain).
PUT("/", updateDomain).
DELETE("/:id", deleteDomain)
records := groupV1.Group("/records")
records.
GET("/:domain", s.getRecords).
POST("/:domain", s.createRecord).
POST("/:domain/bulk", s.createRecords).
PUT("/:domain", s.updateRecord).
DELETE("/:domain/:id", s.deleteRecord)
GET("/:domain", getRecords).
POST("/:domain", createRecord).
POST("/:domain/bulk", createRecords).
PUT("/:domain", updateRecord).
DELETE("/:domain/:id", deleteRecord)
server := s.webServer.Group(s.prefix)
server.Use(func(ctx *gin.Context) {