diff --git a/go-backend/internal/controllers/membershipController.go b/go-backend/internal/controllers/membershipController.go index 5fadcb6..75d6147 100644 --- a/go-backend/internal/controllers/membershipController.go +++ b/go-backend/internal/controllers/membershipController.go @@ -132,7 +132,7 @@ func (mc *MembershipController) DeleteSubscription(c *gin.Context) { if err := mc.Service.DeleteSubscription(&data.Subscription.ID, &data.Subscription.Name); err != nil { if err == errors.ErrNoData { utils.RespondWithError(c, err, "Missing subscription name during deletion", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) - } else if err == errors.ErrNotFound { + } else if err == errors.ErrSubscriptionNotFound { utils.RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) } else if err == errors.ErrInvalidSubscriptionData { utils.RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) diff --git a/go-backend/internal/controllers/membershipController_test.go b/go-backend/internal/controllers/membershipController_test.go index 0f6d24a..840809c 100644 --- a/go-backend/internal/controllers/membershipController_test.go +++ b/go-backend/internal/controllers/membershipController_test.go @@ -251,7 +251,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest { return []UpdateSubscriptionTest{ { Name: "Modified Monthly Fee, should fail", - WantResponse: http.StatusNotAcceptable, + WantResponse: http.StatusBadRequest, WantDBData: map[string]interface{}{"name": "Premium", "monthly_fee": "12"}, Assert: true, Input: GenerateInputJSON( @@ -262,7 +262,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest { }, { Name: "Missing ID, should fail", - WantResponse: http.StatusNotAcceptable, + WantResponse: http.StatusBadRequest, WantDBData: map[string]interface{}{"name": "Premium"}, Assert: true, Input: GenerateInputJSON( @@ -273,7 +273,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest { }, { Name: "Modified Hourly Rate, should fail", - WantResponse: http.StatusNotAcceptable, + WantResponse: http.StatusBadRequest, WantDBData: map[string]interface{}{"name": "Premium", "hourly_rate": "14"}, Assert: true, Input: GenerateInputJSON( @@ -284,7 +284,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest { }, { Name: "IncludedPerYear changed, should fail", - WantResponse: http.StatusNotAcceptable, + WantResponse: http.StatusBadRequest, WantDBData: map[string]interface{}{"name": "Premium", "included_per_year": "0"}, Assert: true, Input: GenerateInputJSON( @@ -295,7 +295,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest { }, { Name: "IncludedPerMonth changed, should fail", - WantResponse: http.StatusNotAcceptable, + WantResponse: http.StatusBadRequest, WantDBData: map[string]interface{}{"name": "Premium", "included_per_month": "1"}, Assert: true, Input: GenerateInputJSON( @@ -306,7 +306,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest { }, { Name: "Update non-existent subscription should fail", - WantResponse: http.StatusNotAcceptable, + WantResponse: http.StatusNotFound, WantDBData: map[string]interface{}{"name": "NonExistentSubscription"}, Assert: false, Input: GenerateInputJSON( @@ -343,7 +343,7 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest { return []DeleteSubscriptionTest{ { Name: "Delete non-existent subscription should fail", - WantResponse: http.StatusExpectationFailed, + WantResponse: http.StatusNotFound, WantDBData: map[string]interface{}{"name": "NonExistentSubscription"}, Assert: false, Input: GenerateInputJSON( diff --git a/go-backend/internal/services/membership_service.go b/go-backend/internal/services/membership_service.go index dc5f026..71a1359 100644 --- a/go-backend/internal/services/membership_service.go +++ b/go-backend/internal/services/membership_service.go @@ -33,11 +33,12 @@ func (service *MembershipService) UpdateSubscription(subscription *models.Subscr existingSubscription, err := repositories.GetSubscriptionByName(&subscription.Name) if err != nil { + if err.Error() == "record not found" { + return nil, errors.ErrSubscriptionNotFound + } return nil, err } - if existingSubscription == nil { - return nil, errors.ErrSubscriptionNotFound - } + if existingSubscription.MonthlyFee != subscription.MonthlyFee || existingSubscription.HourlyRate != subscription.HourlyRate || existingSubscription.Conditions != subscription.Conditions || @@ -56,11 +57,12 @@ func (service *MembershipService) DeleteSubscription(id *uint, name *string) err } exists, err := repositories.GetSubscriptionByName(name) if err != nil { + if err.Error() == "record not found" { + return errors.ErrSubscriptionNotFound + } return err } - if exists == nil { - return errors.ErrNotFound - } + if *id != exists.ID { return errors.ErrInvalidSubscriptionData } diff --git a/go-backend/pkg/errors/errors.go b/go-backend/pkg/errors/errors.go index 4adce96..72b404b 100644 --- a/go-backend/pkg/errors/errors.go +++ b/go-backend/pkg/errors/errors.go @@ -68,7 +68,7 @@ var Responses = struct { PasswordAlreadyChanged: "server.validation.password_already_changed", NoAuthToken: "server.error.no_auth_token", NotFound: "server.error.not_found", - inUse: "server.error.in_use", + InUse: "server.error.in_use", }, Fields: ValidationFields{ General: "general",