Skip to content

Commit

Permalink
application refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlettmiss committed Apr 26, 2024
1 parent 5895cda commit ea4ec8e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 145 deletions.
29 changes: 23 additions & 6 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
petService "github.com/scarlettmiss/petJournal/application/services/petService"
recordService "github.com/scarlettmiss/petJournal/application/services/recordService"
userService "github.com/scarlettmiss/petJournal/application/services/userService"
"github.com/scarlettmiss/petJournal/repositories/petrepo"
"github.com/scarlettmiss/petJournal/repositories/recordrepo"
"github.com/scarlettmiss/petJournal/repositories/userrepo"
)

/*
Expand All @@ -23,9 +26,9 @@ type application struct {
}

type Options struct {
PetService petService.Service
UserService userService.Service
RecordService recordService.Service
PetRepo petrepo.Repository
UserRepo userrepo.Repository
RecordRepo recordrepo.Repository
}

type Application interface {
Expand All @@ -52,10 +55,24 @@ type Application interface {
DeleteRecordUserPet(uId uuid.UUID, pId uuid.UUID, id uuid.UUID) error
}

func New(opts Options) Application {
app := application{petService: opts.PetService, userService: opts.UserService, recordService: opts.RecordService}
func New(opts Options) (Application, error) {
//init services
ps, err := petService.New(opts.PetRepo)
if err != nil {
return nil, err
}
us, err := userService.New(opts.UserRepo)
if err != nil {
return nil, err
}
rs, err := recordService.New(opts.RecordRepo)
if err != nil {
return nil, err
}

app := application{petService: ps, userService: us, recordService: rs}

return &app
return &app, nil
}

func (a *application) CreateUser(opts services.UserCreateOptions) (user.User, string, error) {
Expand Down
28 changes: 0 additions & 28 deletions server/application/services/petService/interfaces.go

This file was deleted.

18 changes: 16 additions & 2 deletions server/application/services/petService/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ import (
"github.com/google/uuid"
"github.com/scarlettmiss/petJournal/application/domain/pet"
"github.com/scarlettmiss/petJournal/application/services"
"github.com/scarlettmiss/petJournal/repositories/petrepo"
textUtils "github.com/scarlettmiss/petJournal/utils/text"
)

type Service interface {
Pet(id uuid.UUID) (pet.Pet, error)
PetByUser(uid uuid.UUID, id uuid.UUID, includeDel bool) (pet.Pet, error)
Pets(includeDel bool) ([]pet.Pet, error)
PetsByUser(userId uuid.UUID, includeDel bool) (map[uuid.UUID]pet.Pet, error)
CreatePet(opts services.PetCreateOptions) (pet.Pet, error)
UpdatePet(opts services.PetUpdateOptions) (pet.Pet, error)
DeletePet(uId uuid.UUID, id uuid.UUID) error
removeVet(id uuid.UUID) error
petsByOwner(userId uuid.UUID, includeDel bool) (map[uuid.UUID]pet.Pet, error)
petByOwner(uid uuid.UUID, id uuid.UUID, includeDel bool) (pet.Pet, error)
}

type service struct {
repo Repository
repo petrepo.Repository
}

func New(repo Repository) (Service, error) {
func New(repo petrepo.Repository) (Service, error) {
return service{repo: repo}, nil
}

Expand Down
27 changes: 0 additions & 27 deletions server/application/services/recordService/interfaces.go

This file was deleted.

16 changes: 14 additions & 2 deletions server/application/services/recordService/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ import (
"github.com/samber/lo"
"github.com/scarlettmiss/petJournal/application/domain/record"
"github.com/scarlettmiss/petJournal/application/services"
"github.com/scarlettmiss/petJournal/repositories/recordrepo"
textUtils "github.com/scarlettmiss/petJournal/utils/text"
"time"
)

type Service interface {
record(id uuid.UUID) (record.Record, error)
PetsRecords(pIds []uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error)
PetRecords(pId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error)
PetRecord(pId uuid.UUID, rId uuid.UUID, includeDel bool) (record.Record, error)
CreateRecord(opts services.RecordCreateOptions) (record.Record, error)
CreateRecords(opts services.RecordsCreateOptions) (map[uuid.UUID]record.Record, error)
UpdateRecord(opts services.RecordUpdateOptions) (record.Record, error)
DeleteRecord(id uuid.UUID) error
}

type service struct {
repo Repository
repo recordrepo.Repository
}

func New(repo Repository) (Service, error) {
func New(repo recordrepo.Repository) (Service, error) {
return service{repo: repo}, nil
}

Expand Down
28 changes: 0 additions & 28 deletions server/application/services/userService/interfaces.go

This file was deleted.

18 changes: 16 additions & 2 deletions server/application/services/userService/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ import (
"github.com/google/uuid"
"github.com/scarlettmiss/petJournal/application/domain/user"
"github.com/scarlettmiss/petJournal/application/services"
"github.com/scarlettmiss/petJournal/repositories/userrepo"
authUtils "github.com/scarlettmiss/petJournal/utils/authorization"
jwtUtils "github.com/scarlettmiss/petJournal/utils/jwt"
textUtils "github.com/scarlettmiss/petJournal/utils/text"
"regexp"
)

type Service interface {
User(id uuid.UUID) (user.User, error)
Users(includeDel bool) ([]user.User, error)
UsersByType(t user.Type, includeDel bool) ([]user.User, error)
UserByType(id uuid.UUID, t user.Type, includeDel bool) (user.User, error)
CreateUser(user services.UserCreateOptions) (user.User, string, error)
UpdateUser(opts services.UserUpdateOptions, includeDel bool) (user.User, error)
Authenticate(email string, password string) (user.User, string, error)
DeleteUser(id uuid.UUID) error
userByEmail(email string, includeDel bool) (user.User, bool)
checkEmail(email string, id uuid.UUID, includeDel bool) error
}

type service struct {
repo Repository
repo userrepo.Repository
}

func New(repo Repository) (Service, error) {
func New(repo userrepo.Repository) (Service, error) {
return service{repo: repo}, nil
}

Expand Down
21 changes: 2 additions & 19 deletions server/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"github.com/scarlettmiss/petJournal/api"
"github.com/scarlettmiss/petJournal/api/config"
"github.com/scarlettmiss/petJournal/application"
petService "github.com/scarlettmiss/petJournal/application/services/petService"
recordService "github.com/scarlettmiss/petJournal/application/services/recordService"
userService "github.com/scarlettmiss/petJournal/application/services/userService"
"github.com/scarlettmiss/petJournal/repositories/petrepo"
"github.com/scarlettmiss/petJournal/repositories/recordrepo"
"github.com/scarlettmiss/petJournal/repositories/userrepo"
Expand Down Expand Up @@ -65,24 +62,10 @@ func main() {

recordsCollection := db.Collection("records")
recordRepo := recordrepo.New(recordsCollection)
//init services
ps, err := petService.New(petRepo)
if err != nil {
panic(err)
}
us, err := userService.New(userRepo)
if err != nil {
panic(err)
}
rs, err := recordService.New(recordRepo)
if err != nil {
panic(err)
}

//pass services to application
opts := application.Options{PetService: ps, UserService: us, RecordService: rs}
app := application.New(opts)

opts := application.Options{PetRepo: petRepo, UserRepo: userRepo, RecordRepo: recordRepo}
app, err := application.New(opts)
if err != nil {
panic(err)
}
Expand Down
28 changes: 18 additions & 10 deletions server/repositories/petrepo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,26 @@ func ConvertToPetDomainModel(dbPet PetDBModel) pet.Pet {
}
}

type Repository struct {
type Repository interface {
CreatePet(pet pet.Pet) (pet.Pet, error)
Pet(id uuid.UUID) (pet.Pet, error)
Pets(includeDel bool) ([]pet.Pet, error)
UpdatePet(pet pet.Pet) (pet.Pet, error)
DeletePet(id uuid.UUID) error
}

type repository struct {
mux sync.Mutex
pets *mongo.Collection
}

func New(collection *mongo.Collection) *Repository {
return &Repository{
func New(collection *mongo.Collection) Repository {
return &repository{
pets: collection,
}
}

func (r *Repository) CreatePet(p pet.Pet) (pet.Pet, error) {
func (r *repository) CreatePet(p pet.Pet) (pet.Pet, error) {
r.mux.Lock()
defer r.mux.Unlock()

Expand Down Expand Up @@ -111,15 +119,15 @@ func (r *Repository) CreatePet(p pet.Pet) (pet.Pet, error) {
return p, nil
}

func (r *Repository) Pet(id uuid.UUID) (pet.Pet, error) {
func (r *repository) Pet(id uuid.UUID) (pet.Pet, error) {
r.mux.Lock()
defer r.mux.Unlock()

retrievedPet, err := r.petInternal(id)
return ConvertToPetDomainModel(retrievedPet), err
}

func (r *Repository) petInternal(id uuid.UUID) (PetDBModel, error) {
func (r *repository) petInternal(id uuid.UUID) (PetDBModel, error) {
var retrievedPet PetDBModel

filter := bson.M{"_id": id}
Expand All @@ -131,7 +139,7 @@ func (r *Repository) petInternal(id uuid.UUID) (PetDBModel, error) {
return retrievedPet, nil
}

func (r *Repository) Pets(includeDel bool) ([]pet.Pet, error) {
func (r *repository) Pets(includeDel bool) ([]pet.Pet, error) {
r.mux.Lock()
defer r.mux.Unlock()

Expand Down Expand Up @@ -175,7 +183,7 @@ func (r *Repository) Pets(includeDel bool) ([]pet.Pet, error) {
return pets, nil
}

func (r *Repository) UpdatePet(p pet.Pet) (pet.Pet, error) {
func (r *repository) UpdatePet(p pet.Pet) (pet.Pet, error) {
r.mux.Lock()
defer r.mux.Unlock()

Expand All @@ -187,7 +195,7 @@ func (r *Repository) UpdatePet(p pet.Pet) (pet.Pet, error) {
return ConvertToPetDomainModel(updatedPet), nil
}

func (r *Repository) updatePetInternal(p PetDBModel) (PetDBModel, error) {
func (r *repository) updatePetInternal(p PetDBModel) (PetDBModel, error) {
// Define the filter to identify the document to update
filter := bson.M{"_id": p.Id}

Expand All @@ -207,7 +215,7 @@ func (r *Repository) updatePetInternal(p PetDBModel) (PetDBModel, error) {
return p, nil
}

func (r *Repository) DeletePet(id uuid.UUID) error {
func (r *repository) DeletePet(id uuid.UUID) error {
r.mux.Lock()
defer r.mux.Unlock()

Expand Down
Loading

0 comments on commit ea4ec8e

Please sign in to comment.