57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package ytrssil
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gitea.theedgeofrage.com/TheEdgeOfRage/ytrssil-api/db"
|
|
"gitea.theedgeofrage.com/TheEdgeOfRage/ytrssil-api/feedparser"
|
|
"gitea.theedgeofrage.com/TheEdgeOfRage/ytrssil-api/models"
|
|
)
|
|
|
|
func (s *server) CreateUser(c *gin.Context) {
|
|
var user models.User
|
|
err := c.BindJSON(&user)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
err = s.handler.CreateUser(c.Request.Context(), user)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"msg": "user created"})
|
|
}
|
|
|
|
func (s *server) SubscribeToChannel(c *gin.Context) {
|
|
var channel models.Channel
|
|
err := c.BindUri(&channel)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
username := c.GetString("username")
|
|
|
|
err = s.handler.SubscribeToChannel(c.Request.Context(), username, channel.ID)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrAlreadySubscribed) {
|
|
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if errors.Is(err, feedparser.ErrInvalidChannelID) {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"msg": "subscribed to channel successfully"})
|
|
}
|