diff --git a/pkg/controllers/subscriptions.go b/pkg/controllers/subscriptions.go index 60e7a49..b4c6f1c 100644 --- a/pkg/controllers/subscriptions.go +++ b/pkg/controllers/subscriptions.go @@ -16,6 +16,7 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr wc := new(SubscriptionController) wc.SubscriptionService = as + s.PUT("/end/:id", wc.End) s.POST("", wc.New) s.PUT("/:id", wc.Edit) s.GET("", wc.GetAll) @@ -56,7 +57,7 @@ func (wc *SubscriptionController) Get(c *gin.Context) { body.Id = auth.(*models.Auth).Id id := c.Param("id") - + embed, _ := c.GetQuery("embed") params.Embed = embed @@ -65,6 +66,19 @@ func (wc *SubscriptionController) Get(c *gin.Context) { c.JSON(200, fr) } +func (wc *SubscriptionController) End(c *gin.Context) { + body := new(models.Auth) + + auth := c.MustGet("auth") + body.Id = auth.(*models.Auth).Id + + id := c.Param("id") + + fr := wc.SubscriptionService.End(c, id) + + c.JSON(200, fr) +} + func (wc *SubscriptionController) GetAll(c *gin.Context) { body := new(models.Auth) auth := c.MustGet("auth") diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go index 23d62cc..55c7ece 100644 --- a/pkg/middleware/cors.go +++ b/pkg/middleware/cors.go @@ -8,7 +8,7 @@ func CORSMiddleware() gin.HandlerFunc { c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") - + if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index fcd930a..3eb1c01 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -114,6 +114,24 @@ func (as *SubscriptionService) Edit(ctx context.Context, body *models.Subscripti return tm } +func (as *SubscriptionService) End(ctx context.Context, id string) *models.Subscription { + db := as.Db.WithContext(ctx) + + tm := new(models.Subscription) + tm.Id = id + tm.EndDate = time.Now() + tm.HasEnd = true + + tx, _ := db.Begin() + defer tx.Rollback() + + tx.Model(tm).WherePK().UpdateNotZero() + + tx.Commit() + + return tm +} + func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) { now := time.Now()