Abstract db handling into repositories
This commit is contained in:
@@ -6,9 +6,9 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"example.com/gin/test/models"
|
||||
"example.com/gin/test/repositories"
|
||||
)
|
||||
|
||||
type CRUDController interface {
|
||||
@@ -25,21 +25,16 @@ type RoomController interface {
|
||||
AddUser(*gin.Context)
|
||||
}
|
||||
|
||||
type roomController struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
type roomController struct {}
|
||||
|
||||
func NewRoomController(db *gorm.DB) RoomController {
|
||||
return &roomController{
|
||||
DB: db,
|
||||
}
|
||||
func NewRoomController() RoomController {
|
||||
return &roomController{}
|
||||
}
|
||||
|
||||
func (rc *roomController) GetAll(c *gin.Context) {
|
||||
var rooms []models.Room
|
||||
result := rc.DB.Find(&rooms)
|
||||
rooms, err := repositories.Rooms.GetAll()
|
||||
|
||||
if result.Error != nil {
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Could not query rooms"))
|
||||
return
|
||||
}
|
||||
@@ -48,18 +43,10 @@ func (rc *roomController) GetAll(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (rc *roomController) GetById(c *gin.Context) {
|
||||
roomId, err := strconv.Atoi(c.Param("id"))
|
||||
room, err := repositories.Rooms.GetById(c.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Room with Id '%s' does not exist", c.Param("id")))
|
||||
return
|
||||
}
|
||||
|
||||
var room models.Room
|
||||
result := rc.DB.First(&room, uint(roomId))
|
||||
|
||||
if result.Error != nil {
|
||||
ReplyError(c, fmt.Errorf("Could not query room: %v", result.Error))
|
||||
ReplyError(c, fmt.Errorf("Could not query room: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -80,9 +67,9 @@ func (rc *roomController) Create(c *gin.Context) {
|
||||
ReplyError(c, err)
|
||||
}
|
||||
|
||||
result := rc.DB.Create(&room)
|
||||
if result.Error != nil {
|
||||
ReplyError(c, fmt.Errorf("Room creation failed: %s", result.Error))
|
||||
_, err = repositories.Rooms.Create(room)
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Room creation failed: %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -114,9 +101,10 @@ func (rc *roomController) Update(c *gin.Context) {
|
||||
}
|
||||
|
||||
room.ID = uint(roomId)
|
||||
result := rc.DB.Save(&room)
|
||||
if result.Error != nil {
|
||||
ReplyError(c, fmt.Errorf("Room creation failed: %s", result.Error))
|
||||
_, err = repositories.Rooms.Update(room)
|
||||
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Room creation failed: %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -124,17 +112,10 @@ func (rc *roomController) Update(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (rc *roomController) Delete(c *gin.Context) {
|
||||
roomId, err := strconv.Atoi(c.Param("id"))
|
||||
err := repositories.Rooms.DeleteById(c.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Room with Id '%s' does not exist", c.Param("id")))
|
||||
return
|
||||
}
|
||||
|
||||
result := rc.DB.Delete(&models.Room{}, roomId)
|
||||
|
||||
if result.Error != nil {
|
||||
ReplyError(c, fmt.Errorf("Room deletion failed: %s", result.Error))
|
||||
ReplyError(c, fmt.Errorf("Room deletion failed: %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -144,23 +125,12 @@ func (rc *roomController) Delete(c *gin.Context) {
|
||||
func (rc *roomController) GetUsers(c *gin.Context) {
|
||||
//only allow room admin
|
||||
|
||||
roomId, err := strconv.Atoi(c.Param("id"))
|
||||
users, err := repositories.Rooms.GetRoomUsersById(c.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Room with Id '%s' does not exist", c.Param("id")))
|
||||
ReplyError(c, fmt.Errorf("Could not get users for room '%s'", c.Param("id")))
|
||||
return
|
||||
}
|
||||
|
||||
var room models.Room
|
||||
result := rc.DB.First(&room, uint(roomId))
|
||||
|
||||
if result.Error != nil {
|
||||
ReplyError(c, fmt.Errorf("Could not query room: %v", result.Error))
|
||||
return
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
rc.DB.Model(&room).Association("Users").Find(&users)
|
||||
|
||||
var emails []string
|
||||
for _, user := range users {
|
||||
@@ -188,10 +158,9 @@ func (rc *roomController) AddUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
//lookup requested user
|
||||
var user models.User
|
||||
result := rc.DB.First(&user, "email = ?", body.Email)
|
||||
user, err := repositories.Users.GetByEmail(body.Email)
|
||||
|
||||
if result.Error != nil {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid user",
|
||||
})
|
||||
@@ -199,22 +168,13 @@ func (rc *roomController) AddUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
roomId, err := strconv.Atoi(c.Param("id"))
|
||||
err = repositories.Rooms.AddRoomUserById(c.Param("id"), user)
|
||||
|
||||
if err != nil {
|
||||
ReplyError(c, fmt.Errorf("Room with Id '%s' does not exist", c.Param("id")))
|
||||
ReplyError(c, fmt.Errorf("Could not add user to room."))
|
||||
return
|
||||
}
|
||||
|
||||
var room models.Room
|
||||
result = rc.DB.First(&room, uint(roomId))
|
||||
|
||||
if result.Error != nil {
|
||||
ReplyError(c, fmt.Errorf("Could not query room: %v", result.Error))
|
||||
return
|
||||
}
|
||||
|
||||
rc.DB.Model(&room).Association("Users").Append(&user)
|
||||
ReplyOK(c, "Added User to Room")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user