Skip to content

Commit

Permalink
feat(master): add new api posts
Browse files Browse the repository at this point in the history
  • Loading branch information
thaisonenouvo committed Nov 28, 2023
1 parent 2222570 commit 0df85dd
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 15 deletions.
51 changes: 48 additions & 3 deletions apps/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/communities": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"summary": "Get Communities",
"operationId": "get-communities",
"responses": {
"200": {
"description": "status\": \"success\", data: { \"communities\": []models.Community }, \"code\": 200}",
"schema": {
"type": "string"
}
}
}
}
},
"/posts": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"summary": "Get Posts",
"operationId": "get-posts",
"responses": {
"200": {
"description": "status\": \"success\", data: { \"posts\": []models.Post }, \"code\": 200}",
"schema": {
"type": "string"
}
}
}
}
},
"/sign-in": {
"post": {
"summary": "Sign In",
Expand Down Expand Up @@ -57,17 +95,24 @@ const docTemplate = `{
}
}
}
},
"securityDefinitions": {
"BearerAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "",
Version: "1.0",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
Title: "Meta-Clone",
Description: "API for Meta-Clone",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
Expand Down
50 changes: 49 additions & 1 deletion apps/api/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
{
"swagger": "2.0",
"info": {
"contact": {}
"description": "API for Meta-Clone",
"title": "Meta-Clone",
"contact": {},
"version": "1.0"
},
"paths": {
"/communities": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"summary": "Get Communities",
"operationId": "get-communities",
"responses": {
"200": {
"description": "status\": \"success\", data: { \"communities\": []models.Community }, \"code\": 200}",
"schema": {
"type": "string"
}
}
}
}
},
"/posts": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"summary": "Get Posts",
"operationId": "get-posts",
"responses": {
"200": {
"description": "status\": \"success\", data: { \"posts\": []models.Post }, \"code\": 200}",
"schema": {
"type": "string"
}
}
}
}
},
"/sign-in": {
"post": {
"summary": "Sign In",
Expand Down Expand Up @@ -46,5 +87,12 @@
}
}
}
},
"securityDefinitions": {
"BearerAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}
32 changes: 32 additions & 0 deletions apps/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,34 @@ definitions:
type: object
info:
contact: {}
description: API for Meta-Clone
title: Meta-Clone
version: "1.0"
paths:
/communities:
get:
operationId: get-communities
responses:
"200":
description: 'status": "success", data: { "communities": []models.Community
}, "code": 200}'
schema:
type: string
security:
- BearerAuth: []
summary: Get Communities
/posts:
get:
operationId: get-posts
responses:
"200":
description: 'status": "success", data: { "posts": []models.Post }, "code":
200}'
schema:
type: string
security:
- BearerAuth: []
summary: Get Posts
/sign-in:
post:
operationId: sign-in
Expand All @@ -28,4 +55,9 @@ paths:
schema:
type: string
summary: Sign In
securityDefinitions:
BearerAuth:
in: header
name: Authorization
type: apiKey
swagger: "2.0"
10 changes: 5 additions & 5 deletions apps/api/handlers/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/tranthaison1231/meta-clone/api/services"
)

type Community struct {
Name string
Logo string
}

// @Summary Get Communities
// @ID get-communities
// @Security BearerAuth
// @Success 200 {string} {"status": "success", data: { "communities": []models.Community }, "code": 200}
// @Router /communities [get]
func GetCommunities(c *gin.Context) {
user := c.MustGet("user").(*models.User)

Expand Down
50 changes: 50 additions & 0 deletions apps/api/handlers/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 Posts
// @ID get-posts
// @Security BearerAuth
// @Success 200 {string} {"status": "success", data: { "posts": []models.Post }, "code": 200}
// @Router /posts [get]
func GetPosts(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,
})
}

func CreatePost(c *gin.Context) {
var req models.CreatePostRequest

if err := c.ShouldBindJSON(&req); err != nil {
h.Fail400(c, err.Error())
return
}

post, err := services.CreatePost(models.Post{
Content: req.Content,
OwnerID: c.MustGet("user").(*models.User).ID,
})

if err != nil {
h.Fail400(c, err.Error())
return
}

h.Success(c, gin.H{
"post": post,
})
}
15 changes: 10 additions & 5 deletions apps/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ var (
)

func initRoutes(r *gin.Engine) {
if port != "" {
docs.SwaggerInfo.BasePath = "/"
} else {
docs.SwaggerInfo.BasePath = "/dev"
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.POST("/sign-in", handlers.SignIn)
r.POST("/sign-up", handlers.SignUp)
Expand All @@ -47,6 +42,8 @@ func initRoutes(r *gin.Engine) {
auth.PUT("/me", handlers.UpdateMe)
auth.GET("/chats", handlers.GetChats)
auth.POST("/chats", handlers.CreateChat)
auth.GET("/posts", handlers.GetPosts)
auth.POST("/posts", handlers.CreatePost)
auth.POST("/chats/:chatID/join", handlers.AddMemberToChat)
auth.GET("/communities", handlers.GetCommunities)
auth.POST("/communities", handlers.CreateCommunity)
Expand Down Expand Up @@ -91,10 +88,18 @@ func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.API
return ginLambda.ProxyWithContext(ctx, req)
}

// @title Meta-Clone
// @version 1.0
// @description API for Meta-Clone
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
if port != "" {
server.ListenAndServe()
docs.SwaggerInfo.BasePath = "/"
} else {
lambda.Start(Handler)
docs.SwaggerInfo.BasePath = "/dev"
}
}
2 changes: 1 addition & 1 deletion apps/api/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Auth(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
h.Fail403(c, "No authorization header provided")
h.Fail401(c, "No authorization header provided")
c.Abort()
return
}
Expand Down
1 change: 1 addition & 0 deletions apps/api/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
&models.Chat{},
&models.Message{},
&models.FriendRequest{},
&models.Post{},
)
fmt.Println("👍 Migration complete")
}
12 changes: 12 additions & 0 deletions apps/api/models/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

type Post struct {
Base
Content string `gorm:"not null;type:text" json:"content"`
OwnerID string `gorm:"not null" json:"ownerId"`
Owner User `gorm:"foreignKey:OwnerID;references:ID" json:"owner"`
}

type CreatePostRequest struct {
Content string `json:"content" validate:"required"`
}
1 change: 1 addition & 0 deletions apps/api/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type User struct {
Communities []*Community `gorm:"many2many:user_communities;" json:"communities"`
Friends []*User `gorm:"many2many:user_friends" json:"friends"`
FriendRequests []FriendRequest `gorm:"foreignKey:UserID" json:"friendRequests"`
Posts []*Post `gorm:"foreignKey:OwnerID" json:"posts"`
}

type UserFriend struct {
Expand Down
25 changes: 25 additions & 0 deletions apps/api/services/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package services

import (
"github.com/tranthaison1231/meta-clone/api/db"
"github.com/tranthaison1231/meta-clone/api/models"
)

func GetPosts(userID string) (*[]models.Post, error) {
var posts []models.Post
err := db.DB.Model(&models.Community{}).Where("owner_id = ?", userID).Find(&posts).Error

if err != nil {
return nil, err
}
return &posts, nil
}

func CreatePost(newPost models.Post) (*models.Post, error) {
err := db.DB.Create(&newPost).Error

if err != nil {
return nil, err
}
return &newPost, nil
}

1 comment on commit 0df85dd

@vercel
Copy link

@vercel vercel bot commented on 0df85dd Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.