settings section
This commit is contained in:
104
internal/admin/province/db/postgresql.go
Normal file
104
internal/admin/province/db/postgresql.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"online-back/internal/admin/province"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) province.Repository {
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
func (r *repository) List(ctx context.Context) ([]province.ProvinceDTO, error) {
|
||||
rows, err := r.client.Query(ctx, `SELECT id, title FROM province ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []province.ProvinceDTO
|
||||
for rows.Next() {
|
||||
item, err := scanProvince(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return items, rows.Err()
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (province.ProvinceDTO, error) {
|
||||
row := r.client.QueryRow(ctx, `SELECT id, title FROM province WHERE id = $1`, id)
|
||||
item, err := scanProvince(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return province.ProvinceDTO{}, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto province.CreateProvinceDTO) (province.ProvinceDTO, error) {
|
||||
title, err := json.Marshal(dto.Title)
|
||||
if err != nil {
|
||||
return province.ProvinceDTO{}, err
|
||||
}
|
||||
|
||||
row := r.client.QueryRow(ctx, `INSERT INTO province (title) VALUES ($1) RETURNING id, title`, title)
|
||||
return scanProvince(row)
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto province.UpdateProvinceDTO) (province.ProvinceDTO, error) {
|
||||
title, err := json.Marshal(dto.Title)
|
||||
if err != nil {
|
||||
return province.ProvinceDTO{}, err
|
||||
}
|
||||
|
||||
row := r.client.QueryRow(ctx,
|
||||
`UPDATE province SET title = $1 WHERE id = $2 RETURNING id, title`,
|
||||
title, id,
|
||||
)
|
||||
item, err := scanProvince(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return province.ProvinceDTO{}, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM province WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
func scanProvince(row scannable) (province.ProvinceDTO, error) {
|
||||
var item province.ProvinceDTO
|
||||
var titleJSON []byte
|
||||
if err := row.Scan(&item.Id, &titleJSON); err != nil {
|
||||
return province.ProvinceDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
||||
return province.ProvinceDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
Reference in New Issue
Block a user