From 8b49750d8946d5fdd4e0c2140d1db99165f5785b Mon Sep 17 00:00:00 2001 From: fabiante Date: Sat, 23 Sep 2023 14:40:35 +0200 Subject: [PATCH 1/6] Return domain model after domain creation --- api/server_admin.go | 2 +- app/service.go | 2 +- app/service_impl.go | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/api/server_admin.go b/api/server_admin.go index 49e6bd3..abdce86 100644 --- a/api/server_admin.go +++ b/api/server_admin.go @@ -48,7 +48,7 @@ func (s *Server) SavePURL(ctx *gin.Context) { func (s *Server) CreateDomain(ctx *gin.Context) { domain := ctx.Param("domain") - err := s.admin.CreateDomain(domain) + _, err := s.admin.CreateDomain(domain) switch true { case err == nil: ctx.Status(http.StatusNoContent) diff --git a/app/service.go b/app/service.go index 29d6131..d74d0f7 100644 --- a/app/service.go +++ b/app/service.go @@ -24,7 +24,7 @@ type AdminServiceInterface interface { // CreateDomain creates a new domain. // // ErrBadRequest is returned if the domain already exists. - CreateDomain(domain string) error + CreateDomain(domain string) (*models.Domain, error) // GetDomain returns the domain with the given name. // diff --git a/app/service_impl.go b/app/service_impl.go index eda6759..e14f3cc 100644 --- a/app/service_impl.go +++ b/app/service_impl.go @@ -39,18 +39,19 @@ func (s *service) Resolve(domain, name string) (string, error) { } } -func (s *service) CreateDomain(name string) error { +func (s *service) CreateDomain(name string) (*models.Domain, error) { domain := &models.Domain{ Name: name, } err := s.db.Create(domain).Error - if err != nil { - return mapDBError(err) + switch { + case err != nil: + return nil, mapDBError(err) + default: + return domain, nil } - - return nil } func (s *service) GetDomain(name string) (*models.Domain, error) { From 918d694236437bbb5c733f76a7fea8fc2920bdf3 Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 22 Sep 2023 16:12:07 +0200 Subject: [PATCH 2/6] Add migration for user table --- db/migrations/migrate.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/db/migrations/migrate.go b/db/migrations/migrate.go index 1b79070..1d0bf03 100644 --- a/db/migrations/migrate.go +++ b/db/migrations/migrate.go @@ -69,6 +69,19 @@ var migrationsPostgres = []any{ target varchar(4096) not null, constraint purls_pk2 unique (domain_id, name) +)`, + ), + newMigration("2023-09-22-00000030-CreateTableUsers", `create table users +( + id serial + constraint user_pk + primary key, + created_at timestamp not null, + updated_at timestamp, + deleted_at timestamp, + email varchar(256) not null, + constraint purls_email + unique (email) )`, ), } From 31f5e4a80a701dbab2b1b048cbf554ab9d038189 Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 22 Sep 2023 16:12:11 +0200 Subject: [PATCH 3/6] Add user model --- app/models/user.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 app/models/user.go diff --git a/app/models/user.go b/app/models/user.go new file mode 100644 index 0000000..086a143 --- /dev/null +++ b/app/models/user.go @@ -0,0 +1,9 @@ +package models + +import "gorm.io/gorm" + +type User struct { + gorm.Model + + Email string +} From 5b125182c8f74b74a45a73f36114d5b941a6c4de Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 22 Sep 2023 16:45:14 +0200 Subject: [PATCH 4/6] Add UserService --- app/service.go | 4 ++++ app/service_user.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 app/service_user.go diff --git a/app/service.go b/app/service.go index d74d0f7..31b219f 100644 --- a/app/service.go +++ b/app/service.go @@ -31,3 +31,7 @@ type AdminServiceInterface interface { // ErrNotFound is returned if the domain does not exist. GetDomain(name string) (*models.Domain, error) } + +type UserServiceInterface interface { + CreateUser(email string) error +} diff --git a/app/service_user.go b/app/service_user.go new file mode 100644 index 0000000..bfd0d45 --- /dev/null +++ b/app/service_user.go @@ -0,0 +1,28 @@ +package app + +import ( + "github.com/fabiante/persurl/app/models" + "gorm.io/gorm" +) + +type UserService struct { + db *gorm.DB +} + +func NewUserService(db *gorm.DB) *UserService { + return &UserService{db: db} +} + +func (s *UserService) CreateUser(email string) error { + user := &models.User{ + Email: email, + } + + err := s.db.Create(user).Error + switch { + case err != nil: + return mapDBError(err) + default: + return nil + } +} From 8a8c51464c5119d60d8d2ce2839ceddb82d3091d Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 22 Sep 2023 17:33:54 +0200 Subject: [PATCH 5/6] Add user service function to load user by email --- app/service.go | 1 + app/service_user.go | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/app/service.go b/app/service.go index 31b219f..42c9745 100644 --- a/app/service.go +++ b/app/service.go @@ -34,4 +34,5 @@ type AdminServiceInterface interface { type UserServiceInterface interface { CreateUser(email string) error + GetUser(email string) (*models.User, error) } diff --git a/app/service_user.go b/app/service_user.go index bfd0d45..f3b0a46 100644 --- a/app/service_user.go +++ b/app/service_user.go @@ -26,3 +26,15 @@ func (s *UserService) CreateUser(email string) error { return nil } } + +func (s *UserService) GetUser(email string) (*models.User, error) { + user := &models.User{} + + err := s.db.Take(user, "email = ?", email).Error + switch { + case err != nil: + return nil, mapDBError(err) + default: + return user, nil + } +} From 90f5e1f285bde887c4e0ce8042a107aef472bf63 Mon Sep 17 00:00:00 2001 From: fabiante Date: Sat, 23 Sep 2023 11:43:01 +0200 Subject: [PATCH 6/6] Add UserService to api --- api/server.go | 5 +++-- cli/cmds/run.go | 3 ++- tests/http_load_test.go | 3 ++- tests/http_test.go | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/api/server.go b/api/server.go index 06136e1..2315ca0 100644 --- a/api/server.go +++ b/api/server.go @@ -11,10 +11,11 @@ import ( type Server struct { resolver app.ResolveServiceInterface admin app.AdminServiceInterface + user app.UserServiceInterface } -func NewServer(resolver app.ResolveServiceInterface, admin app.AdminServiceInterface) *Server { - return &Server{resolver: resolver, admin: admin} +func NewServer(resolver app.ResolveServiceInterface, admin app.AdminServiceInterface, user app.UserServiceInterface) *Server { + return &Server{resolver: resolver, admin: admin, user: user} } func (s *Server) Resolve(ctx *gin.Context) { diff --git a/cli/cmds/run.go b/cli/cmds/run.go index 3df8657..371799c 100644 --- a/cli/cmds/run.go +++ b/cli/cmds/run.go @@ -25,7 +25,8 @@ func init() { engine := gin.Default() service := app.NewService(database.Gorm) - server := api.NewServer(service, service) + userService := app.NewUserService(database.Gorm) + server := api.NewServer(service, service, userService) api.SetupRouting(engine, server) if err := engine.Run(":8060"); err != nil { log.Fatalf("running api failed: %s", err) diff --git a/tests/http_load_test.go b/tests/http_load_test.go index 67f0ecf..1a7a688 100644 --- a/tests/http_load_test.go +++ b/tests/http_load_test.go @@ -32,7 +32,8 @@ func TestLoadWithHTTPDriver(t *testing.T) { require.NoError(t, err, "truncating tables failed") service := app.NewService(database.Gorm) - server := api.NewServer(service, service) + userService := app.NewUserService(database.Gorm) + server := api.NewServer(service, service, userService) api.SetupRouting(handler, server) testServer := httptest.NewServer(handler) diff --git a/tests/http_test.go b/tests/http_test.go index da7c8e3..bc5983b 100644 --- a/tests/http_test.go +++ b/tests/http_test.go @@ -26,7 +26,8 @@ func TestWithHTTPDriver(t *testing.T) { require.NoError(t, err, "truncating tables failed") service := app.NewService(database.Gorm) - server := api.NewServer(service, service) + userService := app.NewUserService(database.Gorm) + server := api.NewServer(service, service, userService) api.SetupRouting(handler, server) testServer := httptest.NewServer(handler)