-
Notifications
You must be signed in to change notification settings - Fork 0
Equimpment CRUD #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Для миграций можно использовать liquidBase/Goose |
internal/storage/storage.go
Outdated
| func InitPostgres() *sql.DB { | ||
| err := godotenv.Load() | ||
|
|
||
| if err != nil { | ||
| fmt.Println("Ошибка загрузки .env") | ||
| } | ||
|
|
||
| host := os.Getenv("HOST") | ||
| port := os.Getenv("PORT") | ||
| username := os.Getenv("POSTGRES_USERNAME") | ||
| password := os.Getenv("POSTGRES_PASSWORD") | ||
| databaseName := os.Getenv("DBNAME") | ||
|
|
||
| psqlInfo := fmt.Sprintf("host=%s port=%s user=%s "+ | ||
| "password=%s dbname=%s sslmode=disable", | ||
| host, port, username, password, databaseName) | ||
|
|
||
| DB, err := sql.Open("postgres", psqlInfo) | ||
| if err != nil { | ||
| fmt.Println(err.Error()) | ||
| panic("База данных нихуя не работает") | ||
| } | ||
|
|
||
| InitTables(DB) | ||
|
|
||
| return DB | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Добавить помимо функции инициализации структуру, чтобы все конфиги были в одном месте, желательно отдельную папку config/config.go
internal/storage/storage.go
Outdated
| err := godotenv.Load() | ||
|
|
||
| if err != nil { | ||
| fmt.Println("Ошибка загрузки .env") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
на работе в рот накажут
.env.example
Outdated
| @@ -0,0 +1,7 @@ | |||
| HOST="localhost" | |||
| PORT=5432 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
хлопни
internal/storage/storage.go
Outdated
| ) | ||
|
|
||
| func InitPostgres(cfg *config.Config) *pgxpool.Pool { | ||
| ctx := context.Background() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
контекст передавай в аргументе из главного main файла
| "theaesthetics.ru/base/internal/services" | ||
| ) | ||
|
|
||
| func InitRouter(e *echo.Echo, db *pgxpool.Pool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
желательно передавать в аргументе log из мейна
| return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
| "error": err.Error(), | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
функцию для оборачивания ошибки желательно
| ctx := context.Background() | ||
| var equipment models.Equipment | ||
| err := c.Bind(&equipment) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
проверку на уникальность, есть ли в бд запись с таким именем
| @@ -0,0 +1,151 @@ | |||
| -- +goose Up | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
один большой блок для накатки и для откатки
| } | ||
|
|
||
| func (r *EquipmentRepository) CreateEqipment(ctx context.Context, title, image string) error { | ||
| query := `INSERT INTO equipments (title, image) VALUES ($1, $2)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
транзакции добавить
| return nil | ||
| } | ||
|
|
||
| func (r *EquipmentRepository) RemoveEquipment(ctx context.Context, id uint8) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
поменять на Delete желательно
| } | ||
|
|
||
| func (r *EquipmentRepository) UpdateEquipment(ctx context.Context, equipment models.Equipment) error { | ||
| query := `UPDATE equipments SET title = $1, image = $2 WHERE id = $3` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
транзкации добавить
| e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ | ||
| Format: "method=${method}, uri=${uri}, status=${status}\n", | ||
| })) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
будет круто добавить graceful shutdown
…o feat/storage
Подключение PostgreSQL
В этом сервисе работаем без ОРМ/Билдеров и прочих вещей, прокачиваем навыки sql
/equipmentsGET -
/equipmentsсписок всего инвентаряGET -
/equipments/:idполучение инвентаря по айдиPOST -
/equipmentsсоздание инвентаряPUT-
/equipments/:idобновление инвентаряDELETE -
/equipments/:idудаление инвентаряДеплой
http://api.theaesthetics.ru:8080/api/v1/