Allow adding users to rooms
This commit is contained in:
@@ -21,6 +21,8 @@ type CRUDController interface {
|
||||
|
||||
type RoomController interface {
|
||||
CRUDController
|
||||
GetUsers(*gin.Context)
|
||||
AddUser(*gin.Context)
|
||||
}
|
||||
|
||||
type roomController struct {
|
||||
@@ -65,7 +67,14 @@ func (rc *roomController) GetById(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (rc *roomController) Create(c *gin.Context) {
|
||||
user, exists := c.Get("user")
|
||||
if !exists {
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
room, err := models.NewRoom(c)
|
||||
room.Admins = append(room.Admins, user.(models.User))
|
||||
|
||||
if err != nil {
|
||||
ReplyError(c, err)
|
||||
@@ -77,6 +86,14 @@ func (rc *roomController) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
//userID := user.(models.User).ID
|
||||
//rc.DB.Model(&models.Room{}).Where("id = ?"), room.ID).Association("Admins").Append(&models.User{ID: userID})
|
||||
|
||||
//if result.Error != nil {
|
||||
// ReplyError(c, fmt.Errorf("Room creation failed: %s", result.Error))
|
||||
// return
|
||||
//}
|
||||
|
||||
ReplyOK(c, "Room was created")
|
||||
}
|
||||
|
||||
@@ -124,6 +141,83 @@ func (rc *roomController) Delete(c *gin.Context) {
|
||||
ReplyOK(c, "Room was deleted")
|
||||
}
|
||||
|
||||
func (rc *roomController) GetUsers(c *gin.Context) {
|
||||
//only allow room admin
|
||||
|
||||
roomId, err := strconv.Atoi(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))
|
||||
return
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
rc.DB.Model(&room).Association("Users").Find(&users)
|
||||
|
||||
var emails []string
|
||||
for _, user := range users {
|
||||
emails = append(emails, user.Email)
|
||||
}
|
||||
|
||||
ReplyOK(c, emails)
|
||||
}
|
||||
|
||||
func (rc *roomController) AddUser(c *gin.Context) {
|
||||
//only allow room admin
|
||||
|
||||
var body struct {
|
||||
Email string
|
||||
}
|
||||
|
||||
err := c.Bind(&body)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Failed to read body",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//lookup requested user
|
||||
var user models.User
|
||||
result := rc.DB.First(&user, "email = ?", body.Email)
|
||||
|
||||
if result.Error != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid user",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
roomId, err := strconv.Atoi(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))
|
||||
return
|
||||
}
|
||||
|
||||
rc.DB.Model(&room).Association("Users").Append(&user)
|
||||
ReplyOK(c, "Added User to Room")
|
||||
}
|
||||
|
||||
func ReplyError(ctx *gin.Context, err error) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import(
|
||||
"os"
|
||||
"fmt"
|
||||
"time"
|
||||
"net/http"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -25,9 +26,10 @@ func NewUserController(db *gorm.DB) UserController {
|
||||
}
|
||||
|
||||
|
||||
func (uc *UserController) Signup(c *gin.Context) {
|
||||
func (uc *UserController) Register(c *gin.Context) {
|
||||
//Get the email/passwd off req body
|
||||
var body struct {
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
@@ -54,10 +56,11 @@ func (uc *UserController) Signup(c *gin.Context) {
|
||||
}
|
||||
|
||||
//create user
|
||||
user := models.User{Email: body.Email, Password: string(hash)}
|
||||
user := models.User{Name: body.Name, Email: body.Email, Password: string(hash)}
|
||||
result := uc.DB.Create(&user)
|
||||
|
||||
if result.Error != nil {
|
||||
fmt.Println("Error: ", result.Error)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Failed to create user",
|
||||
})
|
||||
@@ -136,9 +139,15 @@ func (uc *UserController) Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (uc *UserController) Validate(c *gin.Context) {
|
||||
user, _ := c.Get("user")
|
||||
user, exists := c.Get("user")
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": user,
|
||||
})
|
||||
if exists {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("Logged in with userID: %d", user.(models.User).ID),
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Currently not logged in.",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user