From 4a16acc87e0736dde2ce5253c6106adf253003b7 Mon Sep 17 00:00:00 2001 From: KamilM1205 Date: Tue, 16 Sep 2025 18:27:04 +0400 Subject: [PATCH] Added repositories --- internal/infrastructure/db.go | 19 ---- internal/infrastructure/db/db.go | 23 +++++ .../infrastructure/db/repo/images_repo.go | 58 ++++++++++++ internal/infrastructure/db/repo/post_repo.go | 88 +++++++++++++++++ internal/infrastructure/db/repo/user_repo.go | 94 +++++++++++++++++++ internal/infrastructure/infrastructure.go | 20 ---- migrations/create.sql | 1 + 7 files changed, 264 insertions(+), 39 deletions(-) delete mode 100644 internal/infrastructure/db.go create mode 100644 internal/infrastructure/db/db.go create mode 100644 internal/infrastructure/db/repo/images_repo.go create mode 100644 internal/infrastructure/db/repo/post_repo.go create mode 100644 internal/infrastructure/db/repo/user_repo.go delete mode 100644 internal/infrastructure/infrastructure.go diff --git a/internal/infrastructure/db.go b/internal/infrastructure/db.go deleted file mode 100644 index f83c2ce..0000000 --- a/internal/infrastructure/db.go +++ /dev/null @@ -1,19 +0,0 @@ -package infrastructure - -import ( - "fmt" - - "github.com/jmoiron/sqlx" -) - -type Database struct { - connection *sqlx.DB -} - -func DatabaseInit(config Config) (db Database, err error) { - db = Database{} - db_setup := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s", config.DBUser, config.DBPass, config.DBHost, config.DBPort, config.DBName) - db.connection, err = sqlx.Connect("postgres", db_setup) - - return -} diff --git a/internal/infrastructure/db/db.go b/internal/infrastructure/db/db.go new file mode 100644 index 0000000..ba122cd --- /dev/null +++ b/internal/infrastructure/db/db.go @@ -0,0 +1,23 @@ +package db + +import ( + "58team_blog/internal/infrastructure" + "fmt" + + "github.com/jmoiron/sqlx" +) + +type Database struct { + Conn *sqlx.DB +} + +func DatabaseInit(config infrastructure.Config) (db *Database, err error) { + db = &Database{} + db_setup := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s", config.DBUser, config.DBPass, config.DBHost, config.DBPort, config.DBName) + db.Conn, err = sqlx.Connect("postgres", db_setup) + if err != nil { + return nil, err + } + + return db, nil +} diff --git a/internal/infrastructure/db/repo/images_repo.go b/internal/infrastructure/db/repo/images_repo.go new file mode 100644 index 0000000..0e2256b --- /dev/null +++ b/internal/infrastructure/db/repo/images_repo.go @@ -0,0 +1,58 @@ +package repo + +import ( + "58team_blog/internal/domain/entities" + "58team_blog/internal/infrastructure/db" + + "github.com/google/uuid" + "github.com/jmoiron/sqlx" +) + +type ImagesRepository struct { + conn *db.Database +} + +func CreateImagesRepository(conn *db.Database) ImagesRepository { + return ImagesRepository{ + conn: conn, + } +} + +func (r *ImagesRepository) Create(entity *entities.Images) error { + query := "INSERT INTO " + entities.ImagesTable + "(id, path) VALUES (:id, :path)" + _, err := r.conn.Conn.NamedExec(query, entity) + + return err +} + +func (r *ImagesRepository) FindById(id uuid.UUID) (*entities.Images, error) { + var entity *entities.Images + + query := "SELECT * FROM " + entities.ImagesTable + " WHERE id = ?" + query, args, err := sqlx.In(query, id) + if err != nil { + return nil, err + } + + query = r.conn.Conn.Rebind(query) + err = r.conn.Conn.Get(entity, query, args...) + if err != nil { + return nil, err + } + + return entity, nil +} + +func (r *ImagesRepository) Delete(id uuid.UUID) error { + query := "DELETE FROM " + entities.ImagesTable + " WHERE id=?" + + query, args, err := sqlx.In(query, id) + if err != nil { + return err + } + + query = r.conn.Conn.Rebind(query) + _, err = r.conn.Conn.Query(query, args...) + + return err +} diff --git a/internal/infrastructure/db/repo/post_repo.go b/internal/infrastructure/db/repo/post_repo.go new file mode 100644 index 0000000..5139e96 --- /dev/null +++ b/internal/infrastructure/db/repo/post_repo.go @@ -0,0 +1,88 @@ +package repo + +import ( + "58team_blog/internal/domain/entities" + "58team_blog/internal/infrastructure/db" + + "github.com/google/uuid" + "github.com/jmoiron/sqlx" +) + +type PostRepository struct { + conn *db.Database +} + +func CreatePostRepository(conn *db.Database) PostRepository { + return PostRepository{ + conn: conn, + } +} + +func (r *PostRepository) Create(entity *entities.Post) (*entities.Post, error) { + query := "INSERT INTO " + entities.PostTable + " (id, userId, title, description, content, createdAt, updatedAt)" + + "VALUES (:id, :userId, :title, :description, :content, :createdAt, :updatedAt)" + _, err := r.conn.Conn.NamedExec(query, entity) + + return entity, err +} + +func (r *PostRepository) FindById(id uuid.UUID) (*entities.Post, error) { + var entity *entities.Post + query := "SELECT * FROM " + entities.PostTable + " WHERE id=?" + + query, args, err := sqlx.In(query, id) + if err != nil { + return nil, err + } + + query = r.conn.Conn.Rebind(query) + err = r.conn.Conn.Get(entity, query, args) + + return entity, err +} + +func (r *PostRepository) FindAllByUserName(userName string) ([]*entities.Post, error) { + var entity_list []*entities.Post + query := "SELECT * FROM " + entities.PostTable + " WHERE userId=?" + + query, args, err := sqlx.In(query, userName) + if err != nil { + return nil, err + } + + query = r.conn.Conn.Rebind(query) + err = r.conn.Conn.Select(entity_list, query, args...) + + return entity_list, err +} + +func (r *PostRepository) GetAll() ([]*entities.Post, error) { + var entity_list []*entities.Post + query := "SELECT * FROM " + entities.PostTable + + err := r.conn.Conn.Select(entity_list, query) + + return entity_list, err +} + +func (r *PostRepository) Update(entity *entities.Post) error { + query := "UPDATE " + entities.PostTable + "SET title=:title, description=:description, content=:content, updatedAt=:updatedAt WHERE id=:id" + + _, err := r.conn.Conn.NamedExec(query, entity) + + return err +} + +func (r *PostRepository) Delete(id uuid.UUID) error { + query := "DELETE FROM " + entities.PostTable + " WHERE id=?" + + query, args, err := sqlx.In(query, id) + if err != nil { + return err + } + + query = r.conn.Conn.Rebind(query) + _, err = r.conn.Conn.Exec(query, args...) + + return err +} diff --git a/internal/infrastructure/db/repo/user_repo.go b/internal/infrastructure/db/repo/user_repo.go new file mode 100644 index 0000000..d421407 --- /dev/null +++ b/internal/infrastructure/db/repo/user_repo.go @@ -0,0 +1,94 @@ +package repo + +import ( + "58team_blog/internal/domain/entities" + "58team_blog/internal/infrastructure/db" + + "github.com/google/uuid" + "github.com/jmoiron/sqlx" +) + +type UserRepository struct { + conn *db.Database +} + +func CreateUserRepository(conn *db.Database) UserRepository { + return UserRepository{ + conn: conn, + } +} + +func (r *UserRepository) Create(entity *entities.User) (*entities.User, error) { + query := "INSERT INTO " + entities.UserTable + "(id, username, password) VALUES (:id, :username, :password)" + _, err := r.conn.Conn.NamedExec(query, entity) + + if err != nil { + return nil, err + } + + return entity, nil +} + +func (r *UserRepository) FindById(id uuid.UUID) (*entities.User, error) { + var entity *entities.User + + query := "SELECT * FROM " + entities.UserTable + " WHERE id=?" + query, arg, err := sqlx.In(query, id) + if err != nil { + return nil, err + } + + query = r.conn.Conn.Rebind(query) + err = r.conn.Conn.Get(entity, query, arg...) + + return entity, err +} + +func (r *UserRepository) FindByName(username string) (*entities.User, error) { + var entity *entities.User + + query := "SELECT * FROM " + entities.UserTable + " WHERE username=?" + query, arg, err := sqlx.In(query, username) + if err != nil { + return nil, err + } + + query = r.conn.Conn.Rebind(query) + err = r.conn.Conn.Get(entity, query, arg...) + + return entity, err +} + +func (r *UserRepository) GetAll() ([]*entities.User, error) { + var entity_list []*entities.User + + query := "SELECT * FROM " + entities.UserTable + err := r.conn.Conn.Select(entity_list, query) + if err != nil { + return nil, err + } + + return entity_list, nil +} + +func (r *UserRepository) Update(user *entities.User) error { + query := "UPDATE " + entities.UserTable + " SET username=:username, password=:password WHERE id=:id" + + _, err := r.conn.Conn.NamedExec(query, user) + + return err +} + +func (r *UserRepository) Delete(id uuid.UUID) error { + query := "DELETE FROM " + entities.UserTable + " WHERE id=?" + + query, arg, err := sqlx.In(query, id) + if err != nil { + return err + } + + query = r.conn.Conn.Rebind(query) + _, err = r.conn.Conn.Exec(query, arg...) + + return err +} diff --git a/internal/infrastructure/infrastructure.go b/internal/infrastructure/infrastructure.go deleted file mode 100644 index c10af3a..0000000 --- a/internal/infrastructure/infrastructure.go +++ /dev/null @@ -1,20 +0,0 @@ -package infrastructure - -type Infrastructure struct { - Config Config - Db Database -} - -func InfrastructureInit() (infra Infrastructure, err error) { - infra = Infrastructure{} - - if infra.Config, err = LoadConfig(); err != nil { - return - } - - if infra.Db, err = DatabaseInit(infra.Config); err != nil { - return - } - - return -} diff --git a/migrations/create.sql b/migrations/create.sql index 4e0a1da..ce76e39 100644 --- a/migrations/create.sql +++ b/migrations/create.sql @@ -10,6 +10,7 @@ CREATE TABLE "users" ( CREATE TABLE "post" ( "id" TEXT NOT NULL UNIQUE, + "userId" TEXT NOT NULL UNIQUE, "title" TEXT, "description" TEXT, "content" TEXT,