Added repositories
This commit is contained in:
parent
c0cb826917
commit
4a16acc87e
7 changed files with 264 additions and 39 deletions
|
|
@ -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
|
||||
}
|
||||
23
internal/infrastructure/db/db.go
Normal file
23
internal/infrastructure/db/db.go
Normal file
|
|
@ -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
|
||||
}
|
||||
58
internal/infrastructure/db/repo/images_repo.go
Normal file
58
internal/infrastructure/db/repo/images_repo.go
Normal file
|
|
@ -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
|
||||
}
|
||||
88
internal/infrastructure/db/repo/post_repo.go
Normal file
88
internal/infrastructure/db/repo/post_repo.go
Normal file
|
|
@ -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
|
||||
}
|
||||
94
internal/infrastructure/db/repo/user_repo.go
Normal file
94
internal/infrastructure/db/repo/user_repo.go
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue