Skip to content

Commit

Permalink
Merge pull request #27 from Jaovitosr/21_feat_finances
Browse files Browse the repository at this point in the history
Correção de rotas
  • Loading branch information
yuriBaza23 authored Dec 6, 2023
2 parents e315dc9 + d1f846c commit db8684f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
7 changes: 7 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions internal/api/v1/models/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
14 changes: 7 additions & 7 deletions internal/api/v1/repositories/categoryRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/sql/schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()
Expand Down

0 comments on commit db8684f

Please sign in to comment.