-
Notifications
You must be signed in to change notification settings - Fork 4
feature: added Create for Expense and migrations #20
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
Open
askar200018
wants to merge
8
commits into
main
Choose a base branch
from
feature/db-askar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8396dc3
feature: added Create for Expense and migrations
askar200018 076a337
Merge branch 'main' of https://github.com/talgat-ruby/workout-trainin…
askar200018 38bc2e0
added custom docker file
askar200018 ae554fa
refactor: changed version
askar200018 d68abd9
refactor: refactored config
askar200018 4fec9aa
feature: added delete and create exercise
askar200018 deb9892
feature: added crud for exercises
askar200018 b2d21cd
chore: removed .idea folder
askar200018 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| version: '3.9' | ||
|
|
||
| services: | ||
| postgres: | ||
| container_name: workout_postgres_container | ||
| image: postgres:14.5 | ||
| ports: | ||
| - 5432:5432 | ||
| environment: | ||
| POSTGRES_DB: "workoutdb" | ||
| POSTGRES_PASSWORD: "admin" | ||
| volumes: | ||
| - pgdata:/var/lib/postgresql/data | ||
|
|
||
| volumes: | ||
| pgdata: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package askar_postgres | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| _ "database/sql" | ||
| "fmt" | ||
| "log/slog" | ||
| "time" | ||
| "workout-training-api/internal/askar-postgres/model" | ||
| "workout-training-api/internal/config" | ||
| ) | ||
|
|
||
| type Postgres struct { | ||
| *model.Model | ||
| } | ||
|
|
||
| func New(conf *config.PostgresConfig, logger *slog.Logger) (*Postgres, error) { | ||
| db, err := NewDatabase(conf) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Postgres{ | ||
| Model: model.New(conf, logger.With(slog.String("module", "model")), db), | ||
| }, nil | ||
| } | ||
|
|
||
| func NewDatabase(conf *config.PostgresConfig) (*sql.DB, error) { | ||
| psqlInfo := fmt.Sprintf( | ||
| "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", | ||
| conf.Host, conf.Port, conf.User, conf.Password, conf.Name, | ||
| ) | ||
| db, err := sql.Open("postgres", psqlInfo) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| err = db.PingContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return db, nil | ||
| } |
1 change: 1 addition & 0 deletions
1
internal/askar-postgres/migrations/000001_create_exercises_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DROP TABLE IF EXISTS exercises; |
12 changes: 12 additions & 0 deletions
12
internal/askar-postgres/migrations/000001_create_exercises_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| CREATE TABLE IF NOT EXISTS exercises ( | ||
| id bigserial PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| description TEXT, | ||
| repetitions INTEGER NOT NULL, | ||
| sets INTEGER NOT NULL, | ||
| weight INTEGER NOT NULL, | ||
| muscle_groups text [] NOT NULL, | ||
| categories text [] NOT NULL, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP | ||
| ); |
1 change: 1 addition & 0 deletions
1
internal/askar-postgres/migrations/000002_create_workouts_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DROP TABLE IF EXISTS workouts; |
9 changes: 9 additions & 0 deletions
9
internal/askar-postgres/migrations/000002_create_workouts_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| CREATE TABLE IF NOT EXISTS workouts ( | ||
| id bigserial PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| status VARCHAR(255) NOT NULL, | ||
| scheduled_times TIMESTAMP WITH TIME ZONE[] NOT NULL, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
|
|
1 change: 1 addition & 0 deletions
1
internal/askar-postgres/migrations/000003_exercise_workout_table.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DROP TABLE IF EXISTS exercise_workout; |
15 changes: 15 additions & 0 deletions
15
internal/askar-postgres/migrations/000003_exercise_workout_table.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| CREATE TABLE IF NOT EXISTS exercise_workout ( | ||
| id bigserial PRIMARY KEY, | ||
| exercise_id BIGINT NOT NULL, | ||
| workout_id BIGINT NOT NULL, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, | ||
| CONSTRAINT fk_exercise | ||
| FOREIGN KEY (exercise_id) | ||
| REFERENCES exercises (id) | ||
| ON DELETE CASCADE, | ||
| CONSTRAINT fk_workout | ||
| FOREIGN KEY (workout_id) | ||
| REFERENCES workouts (id) | ||
| ON DELETE CASCADE | ||
| ); |
3 changes: 3 additions & 0 deletions
3
internal/askar-postgres/migrations/000004_add_indexes_to_exercise_workout.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| DROP INDEX IF EXISTS idx_exercise_workout_exercise_id; | ||
| DROP INDEX IF EXISTS idx_exercise_workout_workout_id; | ||
|
|
2 changes: 2 additions & 0 deletions
2
internal/askar-postgres/migrations/000004_add_indexes_to_exercise_workout.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| CREATE INDEX IF NOT EXISTS idx_exercise_workout_exercise_id ON exercise_workout (exercise_id); | ||
| CREATE INDEX IF NOT EXISTS idx_exercise_workout_workout_id ON exercise_workout (workout_id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package exercise | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
| "workout-training-api/internal/types/database" | ||
| ) | ||
|
|
||
| func (m *ExerciseModel) CreateExercise(ctx context.Context, req database.CreateExerciseReq) (database.CreateExerciseResp, error) { | ||
|
|
||
| log := m.logger.With("exercise", "CreateExercise") | ||
|
|
||
| if req != nil { | ||
| log.ErrorContext(ctx, "req is nil") | ||
| return nil, fmt.Errorf("req is nil") | ||
| } | ||
|
|
||
| mdl := &Exercise{ | ||
| Name: req.GetName(), | ||
| Description: req.GetDescription(), | ||
| MuscleGroups: req.GetMuscleGroups(), | ||
| Categories: req.GetCategories(), | ||
| } | ||
|
|
||
| query := ` | ||
| INSERT INTO exercises (name, description, muscle_groups, categories) | ||
| VALUES ($1, $2, $3, $4)` | ||
|
|
||
| args := []interface{}{ | ||
| mdl.Name, mdl.Description, mdl.MuscleGroups, mdl.Categories, | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(ctx, 3*time.Second) | ||
| defer cancel() | ||
|
|
||
| _, err := m.db.ExecContext(ctx, query, args...) | ||
| if err != nil { | ||
| log.ErrorContext(ctx, "failed to execute query", "error", err) | ||
| return nil, fmt.Errorf("failed to execute query: %w", err) | ||
| } | ||
|
|
||
| return true, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package exercise | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "time" | ||
| ) | ||
|
|
||
| type Exercise struct { | ||
| ID int64 | ||
| Name string | ||
| Description string | ||
| MuscleGroups []string | ||
| Categories []string | ||
| CreatedAt time.Time | ||
| UpdatedAt time.Time | ||
| } | ||
|
|
||
| func (e *Exercise) GetID() string { | ||
| return strconv.FormatInt(e.ID, 10) | ||
| } | ||
|
|
||
| func (e *Exercise) GetName() string { | ||
| return e.Name | ||
| } | ||
|
|
||
| func (e *Exercise) GetDescription() string { | ||
| return e.Description | ||
| } | ||
|
|
||
| func (e *Exercise) GetMuscleGroups() []string { | ||
| return e.MuscleGroups | ||
| } | ||
|
|
||
| func (e *Exercise) GetCategories() []string { | ||
| return e.Categories | ||
| } | ||
|
|
||
| func (e *Exercise) GetCreatedAt() time.Time { | ||
| return e.CreatedAt | ||
| } | ||
|
|
||
| func (e *Exercise) GetUpdatedAt() time.Time { | ||
| return e.UpdatedAt | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package exercise | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "log/slog" | ||
| "workout-training-api/internal/config" | ||
| ) | ||
|
|
||
| type ExerciseModel struct { | ||
| conf *config.PostgresConfig | ||
| logger *slog.Logger | ||
| db *sql.DB | ||
| } | ||
|
|
||
| func New(conf *config.PostgresConfig, logger *slog.Logger, db *sql.DB) *ExerciseModel { | ||
| return &ExerciseModel{conf: conf, logger: logger, db: db} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "log/slog" | ||
| "workout-training-api/internal/askar-postgres/model/exercise" | ||
| "workout-training-api/internal/config" | ||
| ) | ||
|
|
||
| type Model struct { | ||
| *exercise.ExerciseModel | ||
| } | ||
|
|
||
| func New(conf *config.PostgresConfig, logger *slog.Logger, db *sql.DB) *Model { | ||
|
|
||
| return &Model{ExerciseModel: exercise.New(conf, logger.With(slog.String("component", "exercise")), db)} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package constant | ||
|
|
||
| type Environment string | ||
|
|
||
| const ( | ||
| EnvironmentLocal Environment = "LOCAL" | ||
| EnvironmentTest Environment = "TEST" | ||
| EnvironmentDev Environment = "DEV" | ||
| EnvironmentProd Environment = "PROD" | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
need postgres 17
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.
8396dc3 поменял версию