133 lines
4.3 KiB
Go
133 lines
4.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"GoMembership/internal/constants"
|
|
"GoMembership/internal/models"
|
|
"GoMembership/internal/services"
|
|
"GoMembership/internal/utils"
|
|
"strings"
|
|
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"GoMembership/pkg/errors"
|
|
"GoMembership/pkg/logger"
|
|
)
|
|
|
|
type MembershipController struct {
|
|
Service services.MembershipServiceInterface
|
|
UserService services.UserServiceInterface
|
|
}
|
|
|
|
func (mc *MembershipController) RegisterSubscription(c *gin.Context) {
|
|
|
|
requestUser, err := mc.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in subscription registrationHandler", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
|
|
if !requestUser.HasPrivilege(constants.Priviliges.Create) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, "Not allowed to register subscription", http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
|
|
var subscription models.Subscription
|
|
if err := c.ShouldBindJSON(&subscription); err != nil {
|
|
utils.HandleValidationError(c, err)
|
|
return
|
|
}
|
|
|
|
// Register Subscription
|
|
id, err := mc.Service.RegisterSubscription(&subscription)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
|
utils.RespondWithError(c, err, "Subscription already exists", http.StatusConflict, errors.Responses.Fields.Subscription, errors.Responses.Keys.Duplicate)
|
|
} else {
|
|
utils.RespondWithError(c, err, "Couldn't register Membershipmodel", http.StatusInternalServerError, errors.Responses.Fields.Subscription, errors.Responses.Keys.InternalServerError)
|
|
}
|
|
return
|
|
}
|
|
logger.Info.Printf("registering subscription: %+v", subscription)
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
"status": "success",
|
|
"id": id,
|
|
})
|
|
}
|
|
|
|
func (mc *MembershipController) UpdateHandler(c *gin.Context) {
|
|
|
|
requestUser, err := mc.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in subscription Updatehandler", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
|
|
if !requestUser.HasPrivilege(constants.Priviliges.Update) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, "Not allowed to update subscription", http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
|
|
var subscription models.Subscription
|
|
if err := c.ShouldBindJSON(&subscription); err != nil {
|
|
utils.HandleValidationError(c, err)
|
|
return
|
|
}
|
|
|
|
// update Subscription
|
|
logger.Info.Printf("Updating subscription %v", subscription.Name)
|
|
id, err := mc.Service.UpdateSubscription(&subscription)
|
|
if err != nil {
|
|
utils.HandleSubscriptionUpdateError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusAccepted, gin.H{
|
|
"status": "success",
|
|
"id": id,
|
|
})
|
|
}
|
|
|
|
func (mc *MembershipController) DeleteSubscription(c *gin.Context) {
|
|
type deleteData struct {
|
|
ID uint `json:"id" binding:"required,numeric,safe_content"`
|
|
Name string `json:"name" binding:"required,safe_content"`
|
|
}
|
|
|
|
var subscription deleteData
|
|
requestUser, err := mc.UserService.FromContext(c)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error extracting user from context in subscription deleteSubscription", http.StatusBadRequest, errors.Responses.Fields.User, errors.Responses.Keys.NoAuthToken)
|
|
return
|
|
}
|
|
|
|
if !requestUser.HasPrivilege(constants.Priviliges.Delete) {
|
|
utils.RespondWithError(c, errors.ErrNotAuthorized, "Not allowed to update subscription", http.StatusUnauthorized, errors.Responses.Fields.User, errors.Responses.Keys.Unauthorized)
|
|
return
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&subscription); err != nil {
|
|
utils.HandleValidationError(c, err)
|
|
return
|
|
}
|
|
|
|
if err := mc.Service.DeleteSubscription(&subscription.ID, &subscription.Name); err != nil {
|
|
utils.HandleSubscriptionDeleteError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Subscription deleted successfully"})
|
|
}
|
|
|
|
func (mc *MembershipController) GetSubscriptions(c *gin.Context) {
|
|
subscriptions, err := mc.Service.GetSubscriptions(nil)
|
|
if err != nil {
|
|
utils.RespondWithError(c, err, "Error retrieving subscriptions", http.StatusInternalServerError, errors.Responses.Fields.Subscription, errors.Responses.Keys.InternalServerError)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"subscriptions": subscriptions,
|
|
})
|
|
}
|