From 44d9b29a0705bf4b30f4d123e9abc39890992750 Mon Sep 17 00:00:00 2001 From: "son.tran" Date: Tue, 28 Nov 2023 16:46:06 +0700 Subject: [PATCH] feat(master): add new api posts --- apps/api/docs/docs.go | 2 +- apps/api/handlers/connect.go | 2 +- apps/api/handlers/disconnect.go | 2 +- apps/api/handlers/feed.go | 27 +++++++++++++++++++++++++++ apps/api/main.go | 4 +--- apps/api/models/media.go | 6 ++++++ 6 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 apps/api/handlers/feed.go create mode 100644 apps/api/models/media.go diff --git a/apps/api/docs/docs.go b/apps/api/docs/docs.go index ea7e99d..ffa98d1 100644 --- a/apps/api/docs/docs.go +++ b/apps/api/docs/docs.go @@ -109,7 +109,7 @@ const docTemplate = `{ var SwaggerInfo = &swag.Spec{ Version: "1.0", Host: "", - BasePath: "", + BasePath: "/dev", Schemes: []string{}, Title: "Meta-Clone", Description: "API for Meta-Clone", diff --git a/apps/api/handlers/connect.go b/apps/api/handlers/connect.go index f6d02a5..5bc0f93 100644 --- a/apps/api/handlers/connect.go +++ b/apps/api/handlers/connect.go @@ -8,7 +8,7 @@ import ( "github.com/aws/aws-lambda-go/events" ) -func Connect(ctx context.Context, event events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) { +func Connect(_ context.Context, event events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) { log.Printf("new connection. id: %s", event.RequestContext.ConnectionID) return events.APIGatewayProxyResponse{ diff --git a/apps/api/handlers/disconnect.go b/apps/api/handlers/disconnect.go index 6092b74..592a1a5 100644 --- a/apps/api/handlers/disconnect.go +++ b/apps/api/handlers/disconnect.go @@ -8,7 +8,7 @@ import ( "github.com/aws/aws-lambda-go/events" ) -func Disconnect(ctx context.Context, event events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) { +func Disconnect(_ context.Context, event events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) { log.Printf("new disconnection. id: %s", event.RequestContext.ConnectionID) return events.APIGatewayProxyResponse{ diff --git a/apps/api/handlers/feed.go b/apps/api/handlers/feed.go new file mode 100644 index 0000000..aeb854c --- /dev/null +++ b/apps/api/handlers/feed.go @@ -0,0 +1,27 @@ +package handlers + +import ( + "github.com/gin-gonic/gin" + h "github.com/tranthaison1231/meta-clone/api/helpers" + "github.com/tranthaison1231/meta-clone/api/models" + "github.com/tranthaison1231/meta-clone/api/services" +) + +// @Summary Get News Feed +// @ID get-news-feed +// @Security BearerAuth +// @Success 200 {string} {"status": "success", data: { "posts": []models.Post }, "code": 200} +// @Router /posts [get] +func GetNewsFeed(c *gin.Context) { + user := c.MustGet("user").(*models.User) + + posts, err := services.GetPosts(user.ID) + + if err != nil { + h.Fail400(c, err.Error()) + } + + h.Success(c, gin.H{ + "posts": posts, + }) +} diff --git a/apps/api/main.go b/apps/api/main.go index f967803..6598a6a 100644 --- a/apps/api/main.go +++ b/apps/api/main.go @@ -22,7 +22,6 @@ import ( "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" - docs "github.com/tranthaison1231/meta-clone/api/docs" ) var ( @@ -42,6 +41,7 @@ func initRoutes(r *gin.Engine) { auth.PUT("/me", handlers.UpdateMe) auth.GET("/chats", handlers.GetChats) auth.POST("/chats", handlers.CreateChat) + auth.POST("/news-feed", handlers.GetNewsFeed) auth.GET("/posts", handlers.GetPosts) auth.POST("/posts", handlers.CreatePost) auth.POST("/chats/:chatID/join", handlers.AddMemberToChat) @@ -97,9 +97,7 @@ func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.API func main() { if port != "" { server.ListenAndServe() - docs.SwaggerInfo.BasePath = "/" } else { lambda.Start(Handler) - docs.SwaggerInfo.BasePath = "/dev" } } diff --git a/apps/api/models/media.go b/apps/api/models/media.go new file mode 100644 index 0000000..fe10293 --- /dev/null +++ b/apps/api/models/media.go @@ -0,0 +1,6 @@ +package models + +type Media struct { + Base + URL string `gorm:"not null;type:text" json:"url"` +}