diff --git a/internal/api/client.go b/internal/api/client.go index 21eb2ff..4c01d78 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -11,16 +11,10 @@ import ( ) func (api *API) CreateClientHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - id, err := uuid.Parse(userId) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } var body types.CreateClientRequestBody @@ -33,7 +27,7 @@ func (api *API) CreateClientHandler(context echo.Context) error { return err } - client, err := api.db.CreateClient(id, body) + client, err := api.db.CreateClient(userId, body) if err != nil { return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error creating User!"}} @@ -43,19 +37,13 @@ func (api *API) CreateClientHandler(context echo.Context) error { } func (api *API) GetAllClientsHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) + userId, err := uuid.Parse(context.Get("id").(string)) - if !ok { + if err != nil { return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } - id, err := uuid.Parse(userId) - - if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} - } - - clients, err := api.db.GetClients(id) + clients, err := api.db.GetClients(userId) if err != nil { return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting Clients!"}} @@ -65,16 +53,10 @@ func (api *API) GetAllClientsHandler(context echo.Context) error { } func (api *API) GetClientHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -93,16 +75,10 @@ func (api *API) GetClientHandler(context echo.Context) error { } func (api *API) UpdateClientHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -137,16 +113,10 @@ func (api *API) UpdateClientHandler(context echo.Context) error { } func (api *API) DeleteClientHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) diff --git a/internal/api/invoice.go b/internal/api/invoice.go index 05ba381..b8914af 100644 --- a/internal/api/invoice.go +++ b/internal/api/invoice.go @@ -11,16 +11,10 @@ import ( ) func (api *API) CreateItemHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -45,16 +39,10 @@ func (api *API) CreateItemHandler(context echo.Context) error { } func (api *API) CreateInvoiceHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - id, err := uuid.Parse(userId) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } var body types.CreateInvoiceRequestBody @@ -67,7 +55,7 @@ func (api *API) CreateInvoiceHandler(context echo.Context) error { return err } - invoice, err := api.db.CreateInvoice(id, body) + invoice, err := api.db.CreateInvoice(userId, body) if err != nil { return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error creating Invoice!"}} @@ -77,13 +65,19 @@ func (api *API) CreateInvoiceHandler(context echo.Context) error { } func (api *API) GetAllItemsHandler(context echo.Context) error { + userId, err := uuid.Parse(context.Get("id").(string)) + + if err != nil { + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} + } + id, err := uuid.Parse(context.Param("id")) if err != nil { return types.Error{Code: http.StatusBadRequest, Messages: []string{"Invoice ID is required to get Items!"}} } - items, err := api.db.GetItems(id) + items, err := api.db.GetItems(userId, id) if err != nil { return types.Error{Code: http.StatusInternalServerError, Messages: []string{"Unexpected error getting Items!"}} @@ -93,7 +87,13 @@ func (api *API) GetAllItemsHandler(context echo.Context) error { } func (api *API) GetAllInvoicesHandler(context echo.Context) error { - invoices, err := api.db.GetInvoices() + userId, err := uuid.Parse(context.Get("id").(string)) + + if err != nil { + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} + } + + invoices, err := api.db.GetInvoices(userId) if err != nil { return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting Invoices!"}} @@ -103,16 +103,10 @@ func (api *API) GetAllInvoicesHandler(context echo.Context) error { } func (api *API) GetItemHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -131,16 +125,10 @@ func (api *API) GetItemHandler(context echo.Context) error { } func (api *API) GetInvoiceHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -159,16 +147,10 @@ func (api *API) GetInvoiceHandler(context echo.Context) error { } func (api *API) UpdateItemHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -199,16 +181,10 @@ func (api *API) UpdateItemHandler(context echo.Context) error { } func (api *API) UpdateInvoiceHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -239,16 +215,10 @@ func (api *API) UpdateInvoiceHandler(context echo.Context) error { } func (api *API) DeleteItemHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) @@ -265,16 +235,10 @@ func (api *API) DeleteItemHandler(context echo.Context) error { } func (api *API) DeleteInvoiceHandler(context echo.Context) error { - userIdString, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - userId, err := uuid.Parse(userIdString) + userId, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } id, err := uuid.Parse(context.Param("id")) diff --git a/internal/api/user.go b/internal/api/user.go index 410c1b0..0e995d6 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -58,16 +58,10 @@ func (api *API) GetAllUsersHandler(context echo.Context) error { } func (api *API) GetUserHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} - } - - id, err := uuid.Parse(userId) + id, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error getting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error getting User!"}} } var user types.User @@ -80,16 +74,10 @@ func (api *API) GetUserHandler(context echo.Context) error { } func (api *API) UpdateUserHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User!"}} - } - - id, err := uuid.Parse(userId) + id, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User!"}} } var body types.UpdateUserRequestBody @@ -118,16 +106,10 @@ func (api *API) UpdateUserHandler(context echo.Context) error { } func (api *API) UpdateUserSecurityCredentialsHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User security credentials!"}} - } - - id, err := uuid.Parse(userId) + id, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User security credentials!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User security credentials!"}} } var body types.UpdateUserSecurityCredentialsBody @@ -150,16 +132,10 @@ func (api *API) UpdateUserSecurityCredentialsHandler(context echo.Context) error } func (api *API) UpdateUserLogoHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User logo!"}} - } - - id, err := uuid.Parse(userId) + id, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error updating User logo!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User logo!"}} } var user types.User @@ -234,16 +210,10 @@ func (api *API) UpdateUserLogoHandler(context echo.Context) error { } func (api *API) DeleteUserHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User logo!"}} - } - - id, err := uuid.Parse(userId) + id, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error deleting User!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error updating User logo!"}} } if err := api.db.DeleteUser(id); err != nil { @@ -254,16 +224,10 @@ func (api *API) DeleteUserHandler(context echo.Context) error { } func (api *API) DeleteUserLogoHandler(context echo.Context) error { - userId, ok := context.Get("id").(string) - - if !ok { - return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error deleting User logo!"}} - } - - id, err := uuid.Parse(userId) + id, err := uuid.Parse(context.Get("id").(string)) if err != nil { - return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error deleting User logo!"}} + return types.Error{Code: http.StatusInternalServerError, Cause: errors.New("Session ID not found after authorization."), Messages: []string{"Unexpected error deleting User logo!"}} } var user types.User diff --git a/internal/models/invoice.go b/internal/models/invoice.go index 9d8069d..f19900b 100644 --- a/internal/models/invoice.go +++ b/internal/models/invoice.go @@ -16,6 +16,7 @@ func (db *DB) CreateItem(userId, invoiceId uuid.UUID, body types.CreateItemReque Name: body.Name, Type: body.Type, Quantity: body.Quantity, + Price: body.Price, Tax: body.Tax, } @@ -135,6 +136,7 @@ func (db *DB) UpdateItem(userId, id uuid.UUID, body types.UpdateItemRequestBody, Name: body.Name, Type: body.Type, Quantity: body.Quantity, + Price: body.Price, Tax: body.Tax, }) diff --git a/internal/models/user.go b/internal/models/user.go index cd640de..7155298 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -13,6 +13,7 @@ func (db *DB) CreateUser(body types.CreateUserRequestBody) (types.User, error) { user := types.User{ ID: uuid.New().String(), SessionID: uuid.New().String(), + Username: strings.ToLower(body.Username), Name: body.Name, FiscalCode: body.FiscalCode, Address: body.Address, @@ -81,6 +82,7 @@ func (db *DB) UpdateUser(id uuid.UUID, body types.UpdateUserRequestBody, user *t } result = db.instance.Model(user).Updates(types.User{ + Username: strings.ToLower(body.Username), Name: body.Name, FiscalCode: body.FiscalCode, Address: body.Address, @@ -88,7 +90,6 @@ func (db *DB) UpdateUser(id uuid.UUID, body types.UpdateUserRequestBody, user *t Country: body.Country, Phone: body.Phone, Email: body.Email, - Username: strings.ToLower(body.Username), }) if result.Error != nil { diff --git a/internal/types/invoice.go b/internal/types/invoice.go index e5cbaef..22f1d9d 100644 --- a/internal/types/invoice.go +++ b/internal/types/invoice.go @@ -78,16 +78,16 @@ type Invoice struct { type CreateItemRequestBody struct { Name string `json:"name" validate:"alpha,required"` Type string `json:"type" validate:"alpha,required"` - Price uint32 `json:"price" validate:"number,required"` Quantity uint32 `json:"quantity" validate:"number,required"` + Price uint32 `json:"price" validate:"number,required"` Tax uint32 `json:"tax" validate:"number,omitempty"` } type UpdateItemRequestBody struct { Name string `json:"name" validate:"alpha,omitempty"` Type string `json:"type" validate:"alpha,omitempty"` - Price uint32 `json:"price" validate:"number,omitempty"` Quantity uint32 `json:"quantity" validate:"number,omitempty"` + Price uint32 `json:"price" validate:"number,omitempty"` Tax uint32 `json:"tax" validate:"number,omitempty"` }