-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add users, blog and comment modules (#2)
- Loading branch information
Showing
17 changed files
with
433 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,8 @@ | |
*.out | ||
|
||
.env | ||
|
||
*.swp | ||
*.lock | ||
|
||
vendor/ |
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,113 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
"github.com/utsav/gin-gonic-boilerplate/db" | ||
"github.com/utsav/gin-gonic-boilerplate/models" | ||
"github.com/utsav/gin-gonic-boilerplate/structs" | ||
) | ||
|
||
//GetBlogs ... | ||
func GetBlogs(c *gin.Context) { | ||
blogs := []models.Blog{} | ||
if err := db.DBCon.Preload("Comments").Preload("User").Find(&blogs).Error; err != nil { | ||
c.JSON(http.StatusNotFound, structs.Error{Code: http.StatusNotFound, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, blogs) | ||
} | ||
|
||
//GetBlog ... | ||
func GetBlog(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "Please pass valid id"}) | ||
return | ||
} | ||
|
||
blog := models.Blog{} | ||
if err := db.DBCon.Preload("Comments").Preload("User").First(&blog, ID).Error; err != nil { | ||
c.JSON(http.StatusNotFound, structs.Error{Code: http.StatusNotFound, Error: "record not found"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, blog) | ||
} | ||
|
||
//CreateBlog ... | ||
func CreateBlog(c *gin.Context) { | ||
|
||
var createBlog (structs.BlogFields) | ||
if err := c.ShouldBindWith(&createBlog, binding.JSON); err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: err.Error()}) | ||
return | ||
} | ||
|
||
var blog models.Blog | ||
blog.BlogFields = createBlog | ||
if err := db.DBCon.Create(&blog).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, blog) | ||
} | ||
|
||
//UpdateBlog ... | ||
func UpdateBlog(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "Please pass valid id"}) | ||
return | ||
} | ||
|
||
var blog (models.Blog) | ||
blog.ID = uint(ID) | ||
|
||
if err := db.DBCon.First(&blog).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "no blog find with this id"}) | ||
return | ||
} | ||
|
||
var updateBlog structs.UpdateBlogFields | ||
if err := c.ShouldBindWith(&updateBlog, binding.JSON); err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: err.Error()}) | ||
return | ||
} | ||
|
||
blog.UpdateBlogFields = updateBlog | ||
if err := db.DBCon.Model(&blog).Updates(&blog).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, blog) | ||
} | ||
|
||
//DeleteBlog ... | ||
func DeleteBlog(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "Please pass valid id"}) | ||
return | ||
} | ||
|
||
var blog (models.Blog) | ||
blog.ID = uint(ID) | ||
if err := db.DBCon.Delete(&blog).Error; err != nil { | ||
fmt.Println("err", err) | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, structs.Success{Code: http.StatusOK, Message: "blog deleted successfully"}) | ||
} |
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,30 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
"github.com/utsav/gin-gonic-boilerplate/db" | ||
"github.com/utsav/gin-gonic-boilerplate/models" | ||
"github.com/utsav/gin-gonic-boilerplate/structs" | ||
) | ||
|
||
//CreateComment ... | ||
func CreateComment(c *gin.Context) { | ||
|
||
var createComment structs.CommentFields | ||
if err := c.ShouldBindWith(&createComment, binding.JSON); err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: err.Error()}) | ||
return | ||
} | ||
|
||
var comment models.Comment | ||
comment.CommentFields = createComment | ||
if err := db.DBCon.Create(&comment).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, comment) | ||
} |
This file was deleted.
Oops, something went wrong.
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,120 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
"github.com/utsav/gin-gonic-boilerplate/db" | ||
"github.com/utsav/gin-gonic-boilerplate/models" | ||
"github.com/utsav/gin-gonic-boilerplate/structs" | ||
) | ||
|
||
//GetUsers ... | ||
func GetUsers(c *gin.Context) { | ||
users := []models.User{} | ||
if err := db.DBCon.Find(&users).Error; err != nil { | ||
c.JSON(http.StatusNotFound, structs.Error{Code: http.StatusNotFound, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, users) | ||
} | ||
|
||
//GetUser ... | ||
func GetUser(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "Please pass valid id"}) | ||
return | ||
} | ||
|
||
user := models.User{} | ||
if err := db.DBCon.First(&user, ID).Error; err != nil { | ||
c.JSON(http.StatusNotFound, structs.Error{Code: http.StatusNotFound, Error: "record not found"}) | ||
return | ||
} | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, user) | ||
} | ||
|
||
//CreateUser ... | ||
func CreateUser(c *gin.Context) { | ||
|
||
var createUser (structs.UserFields) | ||
if err := c.ShouldBindWith(&createUser, binding.JSON); err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: err.Error()}) | ||
return | ||
} | ||
|
||
var user models.User | ||
user.UserFields = createUser | ||
if err := db.DBCon.Create(&user).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, user) | ||
} | ||
|
||
//UpdateUser ... | ||
func UpdateUser(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "Please pass valid id"}) | ||
return | ||
} | ||
|
||
var user (models.User) | ||
user.ID = uint(ID) | ||
|
||
if err := db.DBCon.First(&user).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "no user find with this id"}) | ||
return | ||
} | ||
|
||
var updateUser (structs.UpdateUserFields) | ||
|
||
if err := c.ShouldBindWith(&updateUser, binding.JSON); err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: err.Error()}) | ||
return | ||
} | ||
|
||
user.UpdateUserFields = updateUser | ||
|
||
if err := db.DBCon.Model(&user).Updates(&user).Error; err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, user) | ||
} | ||
|
||
//DeleteUser ... | ||
func DeleteUser(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "Please pass valid id"}) | ||
return | ||
} | ||
|
||
var user (models.User) | ||
user.ID = uint(ID) | ||
if err := db.DBCon.Delete(&user).Error; err != nil { | ||
fmt.Println("err", err) | ||
c.JSON(http.StatusBadRequest, structs.Error{Code: http.StatusBadRequest, Error: "getting error while processing db request"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, structs.Success{Code: http.StatusOK, Message: "user deleted successfully"}) | ||
} |
Binary file not shown.
Oops, something went wrong.