Skip to content

Commit e73bc59

Browse files
committed
add: constraints, validations and gorm configs
1 parent 0098e9f commit e73bc59

File tree

6 files changed

+59
-21
lines changed

6 files changed

+59
-21
lines changed

database/database.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@ import (
66

77
"gorm.io/driver/postgres"
88
"gorm.io/gorm"
9+
"gorm.io/gorm/schema"
910
)
1011

1112
var Conn *gorm.DB
1213

1314
func InitDatabase() {
1415
var err error
16+
1517
dsn := fmt.Sprintf(
16-
"host=%s user=postgres password=postgres dbname=postgres port=5432",
17-
os.Getenv("PG_HOST"))
18-
Conn, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
18+
"host=%v user=%v password=%v dbname=%v port=%v",
19+
os.Getenv("PG_HOST"), os.Getenv("PG_USER"), os.Getenv("PG_PASS"),
20+
os.Getenv("PG_DBNM"), os.Getenv("PG_PORT"),
21+
)
22+
23+
gormConfig := &gorm.Config{
24+
NamingStrategy: schema.NamingStrategy{
25+
SingularTable: true,
26+
},
27+
}
28+
29+
Conn, err = gorm.Open(postgres.Open(dsn), gormConfig)
1930
if err != nil {
2031
panic("failed to connect database")
2132
}

entities/productEntity.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package entities
22

33
type Product struct {
4-
ProCod int64 `gorm:"primaryKey" json:"id"`
5-
ProName string `json:"name"`
6-
ProPrice float32 `json:"price"`
7-
ProAvailableStock uint `json:"stock"`
4+
Cod int64 `json:"id" gorm:"column:pro_cod; primaryKey; <-:create"`
5+
Name string `json:"name" gorm:"column:pro_name"`
6+
Price float32 `json:"price" gorm:"column:pro_price"`
7+
AvailableStock uint `json:"stock" gorm:"column:pro_available_stock"`
88
}

resources/ddl-database.sql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
create table if not exists products (
1+
create table if not exists product (
22
pro_cod bigint not null generated by default as identity,
33
pro_name varchar not null,
4-
pro_price numeric not null,
4+
pro_price numeric(12, 2) not null,
55
pro_available_stock integer not null,
6-
constraint products_pro_cod_pkey primary key (pro_cod)
6+
7+
constraint products_pro_cod_pkey primary key (pro_cod),
8+
constraint products_pro_name_ukey unique (pro_name),
9+
constraint products_pro_name_check check (pro_name <> ''),
10+
constraint products_pro_price_check check (pro_price >= 0),
11+
constraint products_pro_available_stock_check check (pro_available_stock >= 0)
712
);

resources/dev.env

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
PG_HOST=localhost
1+
PG_HOST=localhost
2+
PG_USER=postgres
3+
PG_PASS=postgres
4+
PG_DBNM=postgres
5+
PG_PORT=5432

resources/docker.env

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
PG_HOST=postgres-go
1+
PG_HOST=postgres-go
2+
PG_USER=postgres
3+
PG_PASS=postgres
4+
PG_DBNM=postgres
5+
PG_PORT=5432

services/productService.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ import (
99

1010
func NewProduct(product *entities.Product) (*entities.Product, error) {
1111
var err error
12-
database.Conn.Create(&product)
13-
if product.ProCod == 0 {
14-
err = errors.New("product was not created")
12+
tx := database.Conn.Create(&product)
13+
if tx.Error != nil {
14+
err = errors.New(tx.Error.Error())
1515
}
1616
return product, err
1717
}
1818

1919
func GetProduct(productId string) (*entities.Product, error) {
2020
var err error
2121
var product *entities.Product
22-
database.Conn.Find(&product, productId)
23-
if product.ProCod == 0 {
22+
tx := database.Conn.Find(&product, productId)
23+
if tx.Error != nil {
24+
err = errors.New(tx.Error.Error())
25+
} else if product.Cod == 0 {
2426
err = errors.New("product not found")
2527
}
2628
return product, err
@@ -29,21 +31,33 @@ func GetProduct(productId string) (*entities.Product, error) {
2931
func GetProducts() (*[]entities.Product, error) {
3032
var err error
3133
var products *[]entities.Product
32-
database.Conn.Find(&products)
33-
if *products == nil {
34+
tx := database.Conn.Order("pro_cod").Find(&products)
35+
if tx.Error != nil {
36+
err = errors.New(tx.Error.Error())
37+
} else if *products == nil {
3438
err = errors.New("products not found")
3539
}
3640
return products, err
3741
}
3842

3943
func UpdateProduct(newProduct *entities.Product, productId string) (*entities.Product, error) {
4044
product, err := GetProduct(productId)
41-
database.Conn.Model(product).Updates(newProduct)
45+
if err == nil {
46+
tx := database.Conn.Model(product).Updates(newProduct)
47+
if tx.Error != nil {
48+
err = errors.New(tx.Error.Error())
49+
}
50+
}
4251
return product, err
4352
}
4453

4554
func DeleteProduct(productId string) (*entities.Product, error) {
4655
product, err := GetProduct(productId)
47-
database.Conn.Delete(product)
56+
if err == nil {
57+
tx := database.Conn.Delete(product)
58+
if tx.Error != nil {
59+
err = errors.New(tx.Error.Error())
60+
}
61+
}
4862
return product, err
4963
}

0 commit comments

Comments
 (0)