Add docker
This commit is contained in:
66
services/entry/pkg/db/postgres/projectsRepository.go
Normal file
66
services/entry/pkg/db/postgres/projectsRepository.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"serverctl/pkg/application/projects"
|
||||
"serverctl/pkg/db"
|
||||
)
|
||||
|
||||
var _ projects.Repository = &ProjectsRepository{}
|
||||
|
||||
type ProjectsRepository struct {
|
||||
db *db.Client
|
||||
}
|
||||
|
||||
func NewProjectsRepository(db *db.Client) projects.Repository {
|
||||
return &ProjectsRepository{db: db}
|
||||
}
|
||||
|
||||
type projectData struct {
|
||||
Name string `json:"name"`
|
||||
MemberIds []int `json:"memberIds"`
|
||||
AdminIds []int `json:"adminIds"`
|
||||
}
|
||||
|
||||
func NewProjectData(project *projects.CreateProject) projectData {
|
||||
return projectData{
|
||||
Name: project.Name,
|
||||
AdminIds: project.AdminIds,
|
||||
MemberIds: project.MemberIds,
|
||||
}
|
||||
}
|
||||
|
||||
func (p ProjectsRepository) Create(ctx context.Context, project *projects.CreateProject) (int, error) {
|
||||
conn := p.db.GetConn(ctx)
|
||||
defer conn.Release()
|
||||
|
||||
var projectId int
|
||||
err := conn.QueryRow(ctx, "insert into sctl_project(data) values ($1) returning id", NewProjectData(project)).Scan(&projectId)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return projectId, nil
|
||||
}
|
||||
|
||||
func (p ProjectsRepository) GetForMemberId(ctx context.Context, memberId int) ([]*projects.Project, error) {
|
||||
conn := p.db.GetConn(ctx)
|
||||
defer conn.Release()
|
||||
|
||||
rows, _ := conn.Query(ctx, "select id, data from sctl_project")
|
||||
projectsArr := make([]*projects.Project, 0)
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
id int
|
||||
data projectData
|
||||
)
|
||||
err := rows.Scan(&id, &data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
projectsArr = append(projectsArr, projects.NewProject(id, data.Name, data.MemberIds, data.AdminIds))
|
||||
}
|
||||
|
||||
return projectsArr, nil
|
||||
}
|
49
services/entry/pkg/db/postgres/usersRepository.go
Normal file
49
services/entry/pkg/db/postgres/usersRepository.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
users2 "serverctl/pkg/application/users"
|
||||
"serverctl/pkg/db"
|
||||
)
|
||||
|
||||
var _ users2.Repository = &usersRepository{}
|
||||
|
||||
type usersRepository struct {
|
||||
databasePool *db.Client
|
||||
}
|
||||
|
||||
func NewUsersRepository(db *db.Client) users2.Repository {
|
||||
return &usersRepository{db}
|
||||
}
|
||||
|
||||
func (u *usersRepository) Create(ctx context.Context, user *users2.CreateUser) (int, error) {
|
||||
var userId int
|
||||
conn := u.databasePool.GetConn(ctx)
|
||||
defer conn.Release()
|
||||
|
||||
conn.QueryRow(ctx, "INSERT INTO sctl_user(email, password_hash) values ($1, $2) RETURNING id", user.Email, user.PasswordHash).Scan(&userId)
|
||||
|
||||
if userId == 0 {
|
||||
return -1, errors.New("could not insert data into users table")
|
||||
}
|
||||
|
||||
return userId, nil
|
||||
}
|
||||
|
||||
func (u *usersRepository) GetByEmail(ctx context.Context, email string, passwordHash string) (*users2.User, error) {
|
||||
conn := u.databasePool.GetConn(ctx)
|
||||
defer conn.Release()
|
||||
|
||||
var id int
|
||||
err := conn.QueryRow(ctx, "select id from sctl_user where email = $1 and password_hash = $2", email, passwordHash).Scan(&id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if id <= 0 {
|
||||
return nil, errors.New("user with that password doesn't exist")
|
||||
}
|
||||
|
||||
return users2.NewUser(id, email), nil
|
||||
}
|
Reference in New Issue
Block a user