Skip to content

Commit 9b0d590

Browse files
author
Ubuntu
committed
added cluster model
1 parent c28d219 commit 9b0d590

File tree

3 files changed

+171
-1
lines changed

3 files changed

+171
-1
lines changed

pkg/controllers/cluster.controller.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
"strings"
7+
"time"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/kriipke/console-api/pkg/models"
11+
"gorm.io/gorm"
12+
)
13+
14+
type PostController struct {
15+
DB *gorm.DB
16+
}
17+
18+
func NewPostController(DB *gorm.DB) PostController {
19+
return PostController{DB}
20+
}
21+
22+
// [...] Create Post Handler
23+
func (pc *PostController) CreatePost(ctx *gin.Context) {
24+
currentUser := ctx.MustGet("currentUser").(models.User)
25+
var payload *models.CreatePostRequest
26+
27+
if err := ctx.ShouldBindJSON(&payload); err != nil {
28+
ctx.JSON(http.StatusBadRequest, err.Error())
29+
return
30+
}
31+
32+
now := time.Now()
33+
newPost := models.Post{
34+
Title: payload.Title,
35+
Content: payload.Content,
36+
Image: payload.Image,
37+
User: currentUser.ID,
38+
CreatedAt: now,
39+
UpdatedAt: now,
40+
}
41+
42+
result := pc.DB.Create(&newPost)
43+
if result.Error != nil {
44+
if strings.Contains(result.Error.Error(), "duplicate key") {
45+
ctx.JSON(http.StatusConflict, gin.H{"status": "fail", "message": "Post with that title already exists"})
46+
return
47+
}
48+
ctx.JSON(http.StatusBadGateway, gin.H{"status": "error", "message": result.Error.Error()})
49+
return
50+
}
51+
52+
ctx.JSON(http.StatusCreated, gin.H{"status": "success", "data": newPost})
53+
}
54+
55+
// [...] Update Post Handler
56+
func (pc *PostController) UpdatePost(ctx *gin.Context) {
57+
postId := ctx.Param("postId")
58+
currentUser := ctx.MustGet("currentUser").(models.User)
59+
60+
var payload *models.UpdatePost
61+
if err := ctx.ShouldBindJSON(&payload); err != nil {
62+
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
63+
return
64+
}
65+
var updatedPost models.Post
66+
result := pc.DB.First(&updatedPost, "id = ?", postId)
67+
if result.Error != nil {
68+
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": "No post with that title exists"})
69+
return
70+
}
71+
now := time.Now()
72+
postToUpdate := models.Post{
73+
Title: payload.Title,
74+
Content: payload.Content,
75+
Image: payload.Image,
76+
User: currentUser.ID,
77+
CreatedAt: updatedPost.CreatedAt,
78+
UpdatedAt: now,
79+
}
80+
81+
pc.DB.Model(&updatedPost).Updates(postToUpdate)
82+
83+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "data": updatedPost})
84+
}
85+
86+
// [...] Get Single Post Handler
87+
func (pc *PostController) FindPostById(ctx *gin.Context) {
88+
postId := ctx.Param("postId")
89+
90+
var post models.Post
91+
result := pc.DB.First(&post, "id = ?", postId)
92+
if result.Error != nil {
93+
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": "No post with that title exists"})
94+
return
95+
}
96+
97+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "data": post})
98+
}
99+
100+
// [...] Get All Posts Handler
101+
func (pc *PostController) FindPosts(ctx *gin.Context) {
102+
var page = ctx.DefaultQuery("page", "1")
103+
var limit = ctx.DefaultQuery("limit", "10")
104+
105+
intPage, _ := strconv.Atoi(page)
106+
intLimit, _ := strconv.Atoi(limit)
107+
offset := (intPage - 1) * intLimit
108+
109+
var posts []models.Post
110+
results := pc.DB.Limit(intLimit).Offset(offset).Find(&posts)
111+
if results.Error != nil {
112+
ctx.JSON(http.StatusBadGateway, gin.H{"status": "error", "message": results.Error})
113+
return
114+
}
115+
116+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "results": len(posts), "data": posts})
117+
}
118+
119+
// [...] Delete Post Handler
120+
func (pc *PostController) DeletePost(ctx *gin.Context) {
121+
postId := ctx.Param("postId")
122+
123+
result := pc.DB.Delete(&models.Post{}, "id = ?", postId)
124+
125+
if result.Error != nil {
126+
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": "No post with that title exists"})
127+
return
128+
}
129+
130+
ctx.JSON(http.StatusNoContent, nil)
131+
}

pkg/migrate/migrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ func init() {
1919
}
2020

2121
func main() {
22-
initializers.DB.AutoMigrate(&models.User{}, &models.Post{})
22+
initializers.DB.AutoMigrate(&models.User{}, &models.Post{}, &models.Cluster{})
2323
fmt.Println("? Migration complete")
2424
}

pkg/models/cluster.model.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
type Cluster struct {
10+
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id,omitempty"`
11+
ApiServerHost string `gorm:"uniqueIndex;not null" json:"api_server_host,omitempty"`
12+
ApiServerPort string `gorm:"uniqueIndex;not null" json:"api_server_port,omitempty"`
13+
Name string `gorm:"not null" json:"name,omitempty"`
14+
Image string `gorm:"not null" json:"image,omitempty"`
15+
AddedBy uuid.UUID `gorm:"not null" json:"added_by,omitempty"`
16+
CreatedAt time.Time `gorm:"not null" json:"created_at,omitempty"`
17+
UpdatedAt time.Time `gorm:"not null" json:"updated_at,omitempty"`
18+
}
19+
20+
type CreateClusterRequest struct {
21+
ApiServerHost string `json:"api_server_host" binding:"required"`
22+
ApiServerPort string `json:"api_server_port" binding:"required"`
23+
Name string `json:"name" binding:"required"`
24+
Image string `json:"image,omitempty"`
25+
AddedBy string `json:"added_by,omitempty"`
26+
CreatedAt time.Time `json:"created_at,omitempty"`
27+
UpdatedAt time.Time `json:"updated_at,omitempty"`
28+
}
29+
30+
type UpdateCluster struct {
31+
ApiServerHost string `json:"api_server_host,omitempty"`
32+
ApiServerPort string `json:"api_server_port,omitempty"`
33+
Name string `json:"name,omitempty"`
34+
Image string `json:"image,omitempty"`
35+
AddedBy string `json:"added_by,omitempty"`
36+
CreateAt time.Time `json:"created_at,omitempty"`
37+
UpdatedAt time.Time `json:"updated_at,omitempty"`
38+
}
39+

0 commit comments

Comments
 (0)