Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add users table #83

Merged
merged 6 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion api/server_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

import "gorm.io/gorm"

type User struct {
gorm.Model

Email string
}
7 changes: 6 additions & 1 deletion app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ 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.
//
// ErrNotFound is returned if the domain does not exist.
GetDomain(name string) (*models.Domain, error)
}

type UserServiceInterface interface {
CreateUser(email string) error
GetUser(email string) (*models.User, error)
}
11 changes: 6 additions & 5 deletions app/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions app/service_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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
}
}

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
}
}
3 changes: 2 additions & 1 deletion cli/cmds/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions db/migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)`,
),
}
3 changes: 2 additions & 1 deletion tests/http_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down