chore: refactoring and fixes for the invoices api

This commit is contained in:
2025-07-28 12:25:22 +01:00
parent 6c37031b00
commit 09ae7e8ce9
6 changed files with 61 additions and 160 deletions
+12 -42
View File
@@ -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"))
+31 -67
View File
@@ -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"))
+12 -48
View File
@@ -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