-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2222570
commit 0df85dd
Showing
11 changed files
with
234 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
0df85dd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
meta-clone-facebook-web – ./apps/facebook-web
meta-clone-facebook-web-thaisoncoderstokyo.vercel.app
meta-clone-facebook-web.vercel.app
meta-clone-facebook-web-git-master-thaisoncoderstokyo.vercel.app