init server
This commit is contained in:
37
internal/account/database.go
Normal file
37
internal/account/database.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
*postgresql.DatabasePostgreSQL
|
||||
}
|
||||
|
||||
func (db *Database) GetAllAccountOfUser(id string) ([]*Account, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.account a
|
||||
WHERE a.id = $1
|
||||
`
|
||||
rows, err := db.Session.Query(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
as := make([]*Account, 0)
|
||||
for rows.Next() {
|
||||
a := Account{}
|
||||
err := rows.Scan(&a.ID, &a.userId, &a.name, &a.provider, &a.CreatedAt, &a.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
as = append(as, &a)
|
||||
}
|
||||
return as, nil
|
||||
}
|
||||
|
||||
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
|
||||
return &Database{db}
|
||||
}
|
||||
76
internal/account/handler.go
Normal file
76
internal/account/handler.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"nos-comptes/handler"
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
"nos-comptes/internal/storage/model"
|
||||
"nos-comptes/internal/storage/validators"
|
||||
"nos-comptes/internal/user"
|
||||
"nos-comptes/internal/utils"
|
||||
utils2 "nos-comptes/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
service *Service
|
||||
db *Database
|
||||
userService *user.Service
|
||||
*handler.Context
|
||||
}
|
||||
|
||||
func (c *Context) GetAllAccountOfUser(gc *gin.Context) {
|
||||
userId := gc.Param("userId")
|
||||
err := c.Validator.VarCtx(gc, userId, "uuid4")
|
||||
if err != nil {
|
||||
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
_, err = c.userService.GetUserById(userId)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
accounts, err := c.service.GetAllAccountOfUser(userId)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAllAccounts: get accounts")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get accounts")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
if len(accounts) == 0 {
|
||||
utils.JSON(gc.Writer, http.StatusNoContent, nil)
|
||||
} else {
|
||||
utils.JSON(gc.Writer, http.StatusOK, accounts)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) CreateAccountOfUser(context *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) DeleteAccountOfUser(context *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) GetSpecificAccountOfUser(context *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func NewHandler(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Context {
|
||||
database := NewDatabase(db)
|
||||
service := NewService(database)
|
||||
userService := user.NewService(user.NewDatabase(db))
|
||||
return &Context{service: service, db: database, userService: userService, Context: ctx}
|
||||
}
|
||||
16
internal/account/model.go
Normal file
16
internal/account/model.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package account
|
||||
|
||||
import "time"
|
||||
|
||||
type Account struct {
|
||||
AccountEditable
|
||||
userId string `json:"userId"`
|
||||
}
|
||||
|
||||
type AccountEditable struct {
|
||||
ID string `json:"id"`
|
||||
name string `json:"name"`
|
||||
provider string `json:"provider"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt *time.Time `json:"updatedAt"`
|
||||
}
|
||||
33
internal/account/service.go
Normal file
33
internal/account/service.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"nos-comptes/internal/storage/dao"
|
||||
"nos-comptes/internal/storage/model"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *Database
|
||||
}
|
||||
|
||||
func (s *Service) GetAllAccountOfUser(userId string) ([]*Account, error) {
|
||||
accounts, err := s.db.GetAllAccountOfUser(userId)
|
||||
if e, ok := err.(*dao.Error); ok {
|
||||
switch {
|
||||
case e.Type == dao.ErrTypeNotFound:
|
||||
return nil, &model.ErrNotFound
|
||||
default:
|
||||
return nil, &model.ErrInternalServer
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, &model.ErrInternalServer
|
||||
}
|
||||
|
||||
if accounts == nil {
|
||||
return nil, &model.ErrNotFound
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func NewService(database *Database) *Service {
|
||||
return &Service{db: database}
|
||||
}
|
||||
Reference in New Issue
Block a user