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 13f0f81 commit 5895cda
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
cmd/server/public*
# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
12 changes: 10 additions & 2 deletions server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ import (

type API struct {
*gin.Engine
app *application.Application
app application.Application
}

func New(application *application.Application, ui embed.FS) *API {
func New(application application.Application, ui embed.FS) *API {
api := &API{
Engine: gin.Default(),
app: application,
}
//CORS only for dev mode. Should be commented out for production
//config := cors.DefaultConfig()
//config.AllowAllOrigins = true
//config.AllowMethods = []string{"GET", "POST", "PATCH", "DELETE"}
//config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}
//
//api.Use(cors.New(config))

api.NoRoute(middlewares.NoRouteMiddleware("/", ui, "public"))

api.POST("/api/auth/register", api.register)
Expand Down
72 changes: 48 additions & 24 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
what the actor can do.
application talks with all the services
*/
type Application struct {
type application struct {
petService petService.Service
userService userService.Service
recordService recordService.Service
Expand All @@ -28,61 +28,85 @@ type Options struct {
RecordService recordService.Service
}

func New(opts Options) *Application {
app := Application{petService: opts.PetService, userService: opts.UserService, recordService: opts.RecordService}
type Application interface {
CreateUser(opts services.UserCreateOptions) (user.User, string, error)
UpdateUser(opts services.UserUpdateOptions, includeDel bool) (user.User, error)
Users(includeDel bool) ([]user.User, error)
UsersByType(t user.Type, includeDel bool) ([]user.User, error)
User(id uuid.UUID) (user.User, error)
UserByType(id uuid.UUID, t user.Type, includeDel bool) (user.User, error)
DeleteUser(id uuid.UUID) error
Authenticate(opts services.LoginOptions) (user.User, string, error)
PetsByUser(uId uuid.UUID, includeDel bool) (map[uuid.UUID]pet.Pet, error)
Pet(id uuid.UUID) (pet.Pet, error)
PetByUser(uId uuid.UUID, id uuid.UUID, includeDel bool) (pet.Pet, error)
DeletePet(uId uuid.UUID, id uuid.UUID) error
CreatePet(opts services.PetCreateOptions) (pet.Pet, error)
UpdatePet(opts services.PetUpdateOptions) (pet.Pet, error)
CreateRecord(opts services.RecordCreateOptions) (record.Record, error)
CreateRecords(opts services.RecordsCreateOptions) (map[uuid.UUID]record.Record, error)
RecordsByUser(uId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error)
RecordsByUserPet(uId uuid.UUID, pId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error)
RecordByUserPet(uId uuid.UUID, pId uuid.UUID, tId uuid.UUID, includeDel bool) (record.Record, error)
UpdateRecord(opts services.RecordUpdateOptions) (record.Record, error)
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}

return &app
}

func (a *Application) CreateUser(opts services.UserCreateOptions) (user.User, string, error) {
func (a *application) CreateUser(opts services.UserCreateOptions) (user.User, string, error) {
return a.userService.CreateUser(opts)
}

func (a *Application) UpdateUser(opts services.UserUpdateOptions, includeDel bool) (user.User, error) {
func (a *application) UpdateUser(opts services.UserUpdateOptions, includeDel bool) (user.User, error) {
return a.userService.UpdateUser(opts, includeDel)
}

func (a *Application) Users(includeDel bool) ([]user.User, error) {
func (a *application) Users(includeDel bool) ([]user.User, error) {
return a.userService.Users(includeDel)
}

func (a *Application) UsersByType(t user.Type, includeDel bool) ([]user.User, error) {
func (a *application) UsersByType(t user.Type, includeDel bool) ([]user.User, error) {
return a.userService.UsersByType(t, includeDel)
}

func (a *Application) User(id uuid.UUID) (user.User, error) {
func (a *application) User(id uuid.UUID) (user.User, error) {
return a.userService.User(id)
}

func (a *Application) UserByType(id uuid.UUID, t user.Type, includeDel bool) (user.User, error) {
func (a *application) UserByType(id uuid.UUID, t user.Type, includeDel bool) (user.User, error) {
return a.userService.UserByType(id, t, includeDel)
}

func (a *Application) DeleteUser(id uuid.UUID) error {
func (a *application) DeleteUser(id uuid.UUID) error {
return a.userService.DeleteUser(id)
}

func (a *Application) Authenticate(opts services.LoginOptions) (user.User, string, error) {
func (a *application) Authenticate(opts services.LoginOptions) (user.User, string, error) {
return a.userService.Authenticate(opts.Email, opts.Password)
}

func (a *Application) PetsByUser(uId uuid.UUID, includeDel bool) (map[uuid.UUID]pet.Pet, error) {
func (a *application) PetsByUser(uId uuid.UUID, includeDel bool) (map[uuid.UUID]pet.Pet, error) {
return a.petService.PetsByUser(uId, includeDel)
}

func (a *Application) Pet(id uuid.UUID) (pet.Pet, error) {
func (a *application) Pet(id uuid.UUID) (pet.Pet, error) {
return a.petService.Pet(id)
}

func (a *Application) PetByUser(uId uuid.UUID, id uuid.UUID, includeDel bool) (pet.Pet, error) {
func (a *application) PetByUser(uId uuid.UUID, id uuid.UUID, includeDel bool) (pet.Pet, error) {
return a.petService.PetByUser(uId, id, includeDel)
}

func (a *Application) DeletePet(uId uuid.UUID, id uuid.UUID) error {
func (a *application) DeletePet(uId uuid.UUID, id uuid.UUID) error {
return a.petService.DeletePet(uId, id)
}

func (a *Application) CreatePet(opts services.PetCreateOptions) (pet.Pet, error) {
func (a *application) CreatePet(opts services.PetCreateOptions) (pet.Pet, error) {
_, err := a.User(opts.OwnerId)
if err != nil {
return pet.Nil, err
Expand All @@ -98,11 +122,11 @@ func (a *Application) CreatePet(opts services.PetCreateOptions) (pet.Pet, error)
return a.petService.CreatePet(opts)
}

func (a *Application) UpdatePet(opts services.PetUpdateOptions) (pet.Pet, error) {
func (a *application) UpdatePet(opts services.PetUpdateOptions) (pet.Pet, error) {
return a.petService.UpdatePet(opts)
}

func (a *Application) CreateRecord(opts services.RecordCreateOptions) (record.Record, error) {
func (a *application) CreateRecord(opts services.RecordCreateOptions) (record.Record, error) {
_, err := a.PetByUser(opts.AdministeredBy, opts.PetId, false)
if err != nil {
return record.Nil, err
Expand All @@ -123,7 +147,7 @@ func (a *Application) CreateRecord(opts services.RecordCreateOptions) (record.Re
return a.recordService.CreateRecord(opts)
}

func (a *Application) CreateRecords(opts services.RecordsCreateOptions) (map[uuid.UUID]record.Record, error) {
func (a *application) CreateRecords(opts services.RecordsCreateOptions) (map[uuid.UUID]record.Record, error) {
_, err := a.PetByUser(opts.AdministeredBy, opts.PetId, false)
if err != nil {
return nil, err
Expand All @@ -143,31 +167,31 @@ func (a *Application) CreateRecords(opts services.RecordsCreateOptions) (map[uui
return a.recordService.CreateRecords(opts)
}

func (a *Application) RecordsByUser(uId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error) {
func (a *application) RecordsByUser(uId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error) {
pets, err := a.PetsByUser(uId, includeDel)
if err != nil {
return nil, err
}
return a.recordService.PetsRecords(lo.Keys[uuid.UUID, pet.Pet](pets), includeDel)
}

func (a *Application) RecordsByUserPet(uId uuid.UUID, pId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error) {
func (a *application) RecordsByUserPet(uId uuid.UUID, pId uuid.UUID, includeDel bool) (map[uuid.UUID]record.Record, error) {
_, err := a.PetByUser(uId, pId, false)
if err != nil {
return nil, err
}
return a.recordService.PetRecords(pId, includeDel)
}

func (a *Application) RecordByUserPet(uId uuid.UUID, pId uuid.UUID, tId uuid.UUID, includeDel bool) (record.Record, error) {
func (a *application) RecordByUserPet(uId uuid.UUID, pId uuid.UUID, tId uuid.UUID, includeDel bool) (record.Record, error) {
_, err := a.PetByUser(uId, pId, false)
if err != nil {
return record.Nil, err
}
return a.recordService.PetRecord(pId, tId, includeDel)
}

func (a *Application) UpdateRecord(opts services.RecordUpdateOptions) (record.Record, error) {
func (a *application) UpdateRecord(opts services.RecordUpdateOptions) (record.Record, error) {
if opts.VerifiedBy != uuid.Nil {
_, err := a.UserByType(opts.VerifiedBy, user.Vet, true)
if err != nil {
Expand All @@ -183,7 +207,7 @@ func (a *Application) UpdateRecord(opts services.RecordUpdateOptions) (record.Re
return a.recordService.UpdateRecord(opts)
}

func (a *Application) DeleteRecordUserPet(uId uuid.UUID, pId uuid.UUID, id uuid.UUID) error {
func (a *application) DeleteRecordUserPet(uId uuid.UUID, pId uuid.UUID, id uuid.UUID) error {
_, err := a.RecordByUserPet(uId, pId, id, false)
if err != nil {
return err
Expand Down

0 comments on commit 5895cda

Please sign in to comment.