Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/protoeditor.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/workout-training-api.iml

This file was deleted.

17 changes: 17 additions & 0 deletions docker-compose.askar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3.9'

services:
postgres:
container_name: workout_postgres_container
image: postgres:17.3
ports:
- 5432:5432
environment:
POSTGRES_DB: "workoutdb"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "admin"
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ require (
golang.org/x/tools v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

)
)
31 changes: 1 addition & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
<<<<<<< HEAD
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
=======
github.com/99designs/gqlgen v0.17.64 h1:BzpqO5ofQXyy2XOa93Q6fP1BHLRjTOeU35ovTEsbYlw=
github.com/99designs/gqlgen v0.17.64/go.mod h1:kaxLetFxPGeBBwiuKk75NxuI1fe9HRvob17In74v/Zc=
github.com/PuerkitoBio/goquery v1.9.3 h1:mpJr/ikUA9/GNJB/DBZcGeFDXUtosHRyRrwh7KGdTG0=
Expand Down Expand Up @@ -114,6 +86,5 @@ google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNI
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
>>>>>>> 12012e7eedb63c6cad288001c5cd429cb4ec7dde
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
48 changes: 48 additions & 0 deletions internal/askar-postgres/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package askar_postgres

import (
"context"
"database/sql"
_ "github.com/lib/pq"
"log/slog"
"time"
"workout-training-api/internal/askar-postgres/model"
"workout-training-api/internal/config"
)

type Postgres struct {
*model.Model
}

func New(conf *config.AskarPostgresConfig, logger *slog.Logger) (*Postgres, error) {
db, err := NewDatabase(conf)
if err != nil {
return nil, err
}
//defer db.Close()

return &Postgres{
Model: model.New(conf, logger.With(slog.String("module", "model")), db),
}, nil
}

func NewDatabase(conf *config.AskarPostgresConfig) (*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", conf.DSN)
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS exercises;
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
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS workouts;
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
);

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS exercise_workout;
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
);
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;

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);
54 changes: 54 additions & 0 deletions internal/askar-postgres/model/exercise/create_exercise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package exercise

import (
"context"
"fmt"
"github.com/lib/pq"
"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(),
Repetitions: req.GetRepetitions(),
Sets: req.GetSets(),
Weight: req.GetWeight(),
MuscleGroups: req.GetMuscleGroups(),
Categories: req.GetCategories(),
}

query := `
INSERT INTO exercises (name, description, repetitions, sets, weight, muscle_groups, categories)
VALUES ($1, $2, $3, $4, $5, $6, $7)`

args := []interface{}{
mdl.Name,
mdl.Description,
mdl.Repetitions,
mdl.Sets,
mdl.Weight,
pq.Array(mdl.MuscleGroups),
pq.Array(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
}
43 changes: 43 additions & 0 deletions internal/askar-postgres/model/exercise/delete_exercise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package exercise

import (
"context"
"fmt"
"time"
"workout-training-api/internal/types/database"
)

func (m *ExerciseModel) DeleteExercise(ctx context.Context, req database.DeleteExerciseReq) (database.DeleteExerciseResp, error) {
log := m.logger.With("exercise", "DeleteExercise")

if req == nil {
log.ErrorContext(ctx, "req is nil")
return nil, fmt.Errorf("req is nil")
}

query := `
DELETE FROM exercises
WHERE id = $1`

ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()

result, err := m.db.ExecContext(ctx, query, req.GetID())
if err != nil {
log.ErrorContext(ctx, "failed to delete exercise", "id", req.GetID())
return nil, fmt.Errorf("failed to delete exercise: %w", err)
}

rowsAffected, err := result.RowsAffected()
if err != nil {
log.ErrorContext(ctx, "failed to call rowsAffected", "id", req.GetID())
return nil, fmt.Errorf("failed to call rowsAffected: %w", err)
}

if rowsAffected == 0 {
log.ErrorContext(ctx, "failed to find an exercise with id", "id", req.GetID())
return nil, fmt.Errorf("failed to find an exercise")
}

return true, nil
}
57 changes: 57 additions & 0 deletions internal/askar-postgres/model/exercise/exercise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package exercise

import (
"strconv"
"time"
)

type Exercise struct {
ID int64
Name string
Description string
Repetitions int32
Sets int32
Weight int32
MuscleGroups []string
Categories []string
CreatedAt time.Time
UpdatedAt time.Time
}

func (e *Exercise) GetWorkoutID() string {
return "id"
}

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) GetRepetitions() int32 { return e.Repetitions }

func (e *Exercise) GetSets() int32 { return e.Sets }

func (e *Exercise) GetWeight() int32 { return e.Weight }

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
}
Loading