Add docker

This commit is contained in:
2022-02-14 20:03:53 +01:00
parent 80eb11b0c9
commit e69073ecad
32 changed files with 508 additions and 10 deletions

View File

@@ -0,0 +1,31 @@
package projects
type Project struct {
Id int
Name string
MemberIds []int
AdminIds []int
}
func NewProject(id int, name string, memberIds []int, adminIds []int) *Project {
return &Project{
Id: id,
Name: name,
MemberIds: memberIds,
AdminIds: adminIds,
}
}
type CreateProject struct {
Name string
MemberIds []int
AdminIds []int
}
func NewCreateProject(name string, userId int) *CreateProject {
return &CreateProject{
Name: name,
MemberIds: []int{userId},
AdminIds: []int{userId},
}
}

View File

@@ -0,0 +1,8 @@
package projects
import "context"
type Repository interface {
Create(ctx context.Context, project *CreateProject) (int, error)
GetForMemberId(ctx context.Context, memberId int) ([]*Project, error)
}

View File

@@ -0,0 +1,59 @@
package projects
import (
"context"
"fmt"
"github.com/eko/gocache/cache"
"go.uber.org/zap"
)
type Service struct {
projectsRepository Repository
logger *zap.Logger
cache *cache.MetricCache
}
func NewService(logger *zap.Logger, projectsRepository Repository, cache *cache.MetricCache) *Service {
return &Service{
logger: logger,
projectsRepository: projectsRepository,
cache: cache}
}
func (s *Service) CreateProject(ctx context.Context, userId int, name string) (int, error) {
s.logger.Debug("creating project",
zap.String("name", name),
zap.Int("creatorId", userId))
projectId, err := s.projectsRepository.Create(ctx, NewCreateProject(name, userId))
if err != nil {
s.logger.Warn(err.Error())
return -1, err
}
_ = s.cache.Delete(fmt.Sprintf("projects_userId_%d", userId))
return projectId, nil
}
func (s *Service) Get(ctx context.Context, userId int) ([]*Project, error) {
s.logger.Debug("getting projects",
zap.Int("userId", userId))
loadFunc := func(key interface{}) (interface{}, error) {
s.logger.Debug("getting projects from repository",
zap.Int("userId", userId))
return s.projectsRepository.GetForMemberId(ctx, userId)
}
cacheEntry := cache.NewLoadable(
loadFunc,
s.cache)
entry, err := cacheEntry.Get(fmt.Sprintf("projects_userId_%d", userId))
if err != nil {
return nil, err
}
return entry.([]*Project), nil
}

View File

@@ -0,0 +1,10 @@
package users
type User struct {
Id int
Email string
}
func NewUser(id int, email string) *User {
return &User{id, email}
}

View File

@@ -0,0 +1,8 @@
package users
import "context"
type Repository interface {
Create(ctx context.Context, user *CreateUser) (int, error)
GetByEmail(ctx context.Context, email string, passwordHash string) (*User, error)
}

View File

@@ -0,0 +1,54 @@
package users
import (
"context"
"fmt"
"github.com/eko/gocache/cache"
"go.uber.org/zap"
)
type Service struct {
logger *zap.Logger
cache *cache.MetricCache
repository Repository
passwordHasher PasswordHasher
}
func NewService(l *zap.Logger, ur Repository, c *cache.MetricCache) *Service {
return &Service{
logger: l,
repository: ur,
cache: c,
passwordHasher: NewPlainTextPasswordHasher(),
}
}
func (s *Service) Create(email string, password string) (int, error) {
createUser, err := NewCreateUser(email, password, s.passwordHasher)
if err != nil {
return -1, err
}
var userId int
userId, err = s.repository.Create(context.Background(), createUser)
if err != nil {
s.logger.Warn("Could not create user in service")
return 0, err
}
return userId, nil
}
func (s *Service) Authenticate(ctx context.Context, email string, password string) (*User, error) {
loadFunc := func(key interface{}) (interface{}, error) {
s.logger.Debug("getting user from cache", zap.String("email", email))
return s.repository.GetByEmail(ctx, email, s.passwordHasher.HashPassword(password))
}
get, err := cache.NewLoadable(loadFunc, s.cache).Get(fmt.Sprintf("user_email_%s", email))
if err != nil {
return nil, err
}
return get.(*User), nil
}

View File

@@ -0,0 +1,49 @@
package users
import "errors"
type PasswordHasher interface {
HashPassword(password string) string
}
type CreateUser struct {
Email string
PasswordHash string
}
func NewCreateUser(email string, password string, hasher PasswordHasher) (*CreateUser, error) {
if email == "" {
return nil, errors.New("Email cannot be empty for user")
}
if password == "" || len(password) < 8 {
return nil, errors.New("password is doesn't fit requirements")
}
return &CreateUser{
Email: email,
PasswordHash: hasher.HashPassword(password),
}, nil
}
type bCryptPasswordHasher struct {
}
func NewBCryptPasswordHasher() PasswordHasher {
return &bCryptPasswordHasher{}
}
func (b bCryptPasswordHasher) HashPassword(password string) string {
//TODO implement me
panic("implement me")
}
type plainTextPasswordHasher struct {
}
func NewPlainTextPasswordHasher() PasswordHasher {
return &plainTextPasswordHasher{}
}
func (p plainTextPasswordHasher) HashPassword(password string) string {
return password
}