diff --git a/cmd/server/server.go b/cmd/server/server.go index 87949b6..ebf209f 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -77,6 +77,13 @@ func HttpInit(port string) { // Rotas relacionadas a logins de usuário e incubadora r.HandleFunc("/login", handlers.Login).Methods("POST") + //Rotas relacionadas às categorias de finanças + r.HandleFunc("/category", handlers.CreateCategory).Methods("POST") + r.HandleFunc("/category/company/{id}", handlers.GetAllCategories).Methods("GET") + r.HandleFunc("/category/{id}", handlers.GetCategory).Methods("GET") + r.HandleFunc("/category/{id}", handlers.UpdateCategory).Methods("PUT") + r.HandleFunc("/category/{id}", handlers.DeleteCategory).Methods("DELETE") + handler := cors.Handler(r) http.ListenAndServe(fmt.Sprintf(":%s", port), handler) } diff --git a/internal/api/v1/models/categories.go b/internal/api/v1/models/categories.go index c605bb7..b5c7f76 100644 --- a/internal/api/v1/models/categories.go +++ b/internal/api/v1/models/categories.go @@ -3,6 +3,7 @@ package models type Category struct { ID string `json:"id"` Name string `json:"name"` + Type string `json:"type"` CompanyID string `json:"companyId"` CreatedAt string `json:"createdAt"` UpdatedAt string `json:"updatedAt"` diff --git a/internal/api/v1/repositories/categoryRepository.go b/internal/api/v1/repositories/categoryRepository.go index 04bd8a9..0364fb6 100644 --- a/internal/api/v1/repositories/categoryRepository.go +++ b/internal/api/v1/repositories/categoryRepository.go @@ -30,9 +30,9 @@ func InsertCategory(cat models.Category) (id string, err error) { defer db.Close() - stmt := `INSERT INTO categories (id, name, companyId) VALUES ($1, $2, $3) RETURNING id` + stmt := `INSERT INTO categories (id, name, type, companyId) VALUES ($1, $2, $3, $4) RETURNING id` - err = db.QueryRow(stmt, cat.ID, cat.Name, cat.CompanyID).Scan(&id) + err = db.QueryRow(stmt, cat.ID, cat.Name, cat.Type, cat.CompanyID).Scan(&id) return } @@ -45,7 +45,7 @@ func GetCategory(id string) (cat models.Category, err error) { defer conn.Close() stmt := `SELECT * FROM categories WHERE id=$1` - err = conn.QueryRow(stmt, id).Scan(&cat.ID, &cat.Name, &cat.CompanyID, &cat.CreatedAt, &cat.UpdatedAt) + err = conn.QueryRow(stmt, id).Scan(&cat.ID, &cat.Name, &cat.Type, &cat.CompanyID, &cat.CreatedAt, &cat.UpdatedAt) return } @@ -57,7 +57,7 @@ func GetAllCategories(id string) (cat []models.Category, err error) { } defer conn.Close() - stmt := `SELECT id, name, companyId, createdAt, updatedAt FROM categories WHERE companyId=$1` + stmt := `SELECT id, name, type, companyId, createdAt, updatedAt FROM categories WHERE companyId=$1` catRows, err := conn.Query(stmt, id) if err != nil { return @@ -66,7 +66,7 @@ func GetAllCategories(id string) (cat []models.Category, err error) { for catRows.Next() { var c models.Category - err = catRows.Scan(&c.ID, &c.Name, &c.CompanyID, &c.CreatedAt, &c.UpdatedAt) + err = catRows.Scan(&c.ID, &c.Name, &c.Type, &c.CompanyID, &c.CreatedAt, &c.UpdatedAt) if err != nil { continue } @@ -86,8 +86,8 @@ func UpdateCategory(id string, cat models.Category) (int64, error) { defer conn.Close() - stmt := `UPDATE categories SET name=$1, updatedAt=$2 WHERE id=$3` - row, err := conn.Exec(stmt, cat.Name, time.Now(), id) + stmt := `UPDATE categories SET name=$1, type=$2, updatedAt=$3 WHERE id=$4` + row, err := conn.Exec(stmt, cat.Name, cat.Type, time.Now(), id) if err != nil { return 0, err } diff --git a/scripts/sql/schemas.sql b/scripts/sql/schemas.sql index ad4264a..4070813 100644 --- a/scripts/sql/schemas.sql +++ b/scripts/sql/schemas.sql @@ -61,6 +61,7 @@ create table if not exists finances ( id uuid default gen_random_uuid() primary key, name varchar not null, type varchar not null, + category varchar not null default '', value integer not null default 0, companyId uuid not null, createdAt timestamp default now(), @@ -83,6 +84,7 @@ create table if not exists tasks ( create table if not exists categories( id uuid default gen_random_uuid() primary key, name varchar not null, + type varchar not null default '', companyId uuid not null, createdAt timestamp default now(), updatedAt timestamp default now()