debut ajout compte joint
This commit is contained in:
116
internal/jointaccount/database.go
Normal file
116
internal/jointaccount/database.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package jointaccount
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nos-comptes/internal/storage/dao"
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
"nos-comptes/internal/utils"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
*postgresql.DatabasePostgreSQL
|
||||
}
|
||||
|
||||
func (db *Database) GetAllJointaccountOfUser(id string) ([]*Jointaccount, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.jointaccount a
|
||||
WHERE a.user_id = $1
|
||||
`
|
||||
rows, err := db.Session.Query(q, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
as := make([]*Jointaccount, 0)
|
||||
for rows.Next() {
|
||||
a := Jointaccount{}
|
||||
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 (db *Database) GetJointaccountWithNameForUser(name string, id string) (*Jointaccount, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.jointaccount a
|
||||
WHERE a.user_id = $1
|
||||
AND a.name = $2
|
||||
`
|
||||
row, err := db.Session.Query(q, id, name)
|
||||
if !row.Next() {
|
||||
return nil, dao.NewDAOError(dao.ErrTypeNotFound, fmt.Errorf("No row found"))
|
||||
}
|
||||
a := Jointaccount{}
|
||||
row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
||||
if row.Next() {
|
||||
return nil, fmt.Errorf("Impossibru")
|
||||
}
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return nil, postgresql.HandlePgError(errPq)
|
||||
}
|
||||
if err != nil {
|
||||
utils.GetLogger().Info(err)
|
||||
return nil, err
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (db *Database) CreateJointaccount(jointaccount *Jointaccount) error {
|
||||
q := `
|
||||
INSERT INTO public.jointaccount
|
||||
(Name, Provider, user_id)
|
||||
VALUES
|
||||
($1, $2, $3)
|
||||
RETURNING id, created_at
|
||||
`
|
||||
|
||||
err := db.Session.
|
||||
QueryRow(q, jointaccount.Name, jointaccount.Provider, jointaccount.UserId).
|
||||
Scan(&jointaccount.ID, &jointaccount.CreatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return postgresql.HandlePgError(errPq)
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (db *Database) DeleteJointaccountOfAnUser(userId, jointaccountId string) error {
|
||||
query := `
|
||||
DELETE FROM jointaccount
|
||||
WHERE user_id = $1
|
||||
AND id = $2;`
|
||||
_, err := db.Session.Exec(query, userId, jointaccountId)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) GetASpecificJointaccountForUser(userId, jointaccountId string) (*Jointaccount, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.jointaccount a
|
||||
WHERE a.user_id = $1
|
||||
AND a.id = $2
|
||||
`
|
||||
row := db.Session.QueryRow(q, userId, jointaccountId)
|
||||
a := Jointaccount{}
|
||||
err := row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
||||
if errPq, ok := err.(*pq.Error); ok {
|
||||
return nil, postgresql.HandlePgError(errPq)
|
||||
}
|
||||
if err != nil {
|
||||
utils.GetLogger().Info(err)
|
||||
return nil, err
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
|
||||
return &Database{db}
|
||||
}
|
||||
100
internal/jointaccount/handler.go
Normal file
100
internal/jointaccount/handler.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package jointaccount
|
||||
|
||||
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"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
service *Service
|
||||
db *Database
|
||||
userService *user.Service
|
||||
*handler.Context
|
||||
}
|
||||
|
||||
func (c *Context) GetAllJointaccountOfUser(gc *gin.Context) {
|
||||
userId := gc.Param("userId")
|
||||
jointaccounts, err := c.service.GetAllJointaccountOfUser(userId)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAllJointaccounts: get jointaccounts")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get jointaccounts")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
if len(jointaccounts) == 0 {
|
||||
utils.JSON(gc.Writer, http.StatusNoContent, nil)
|
||||
} else {
|
||||
utils.JSON(gc.Writer, http.StatusOK, jointaccounts)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) CreateJointaccountOfUser(gc *gin.Context) {
|
||||
userId := gc.Param("userId")
|
||||
|
||||
var jointaccount Jointaccount
|
||||
var jointaccountEditable JointaccountEditable
|
||||
if err := gc.BindJSON(&jointaccountEditable); err != nil {
|
||||
utils.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
jointaccount = Jointaccount{JointaccountEditable: jointaccountEditable, UserId: userId}
|
||||
jointaccountFound, err := c.service.GetJointaccountWithNameForUser(jointaccount.Name, userId)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
if e.Type != model.ErrNotFound.Type {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetJointaccount: get jointaccount error")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
return
|
||||
}
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get jointaccount")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
if jointaccountFound != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(&model.ErrAlreadyExists).WithField("type", model.ErrAlreadyExists.Type).Error("error CreateJointaccount: jointaccount already exists")
|
||||
utils.JSONErrorWithMessage(gc.Writer, model.ErrAlreadyExists, "jointaccount already exists with the same Name")
|
||||
return
|
||||
}
|
||||
jointaccount.UserId = userId
|
||||
jointaccountSaved, err := c.service.CreateJointaccount(jointaccount)
|
||||
if err != nil {
|
||||
utils.GetLogger().Info(err)
|
||||
utils.JSONErrorWithMessage(gc.Writer, model.ErrInternalServer, err.Error())
|
||||
return
|
||||
}
|
||||
utils.JSON(gc.Writer, http.StatusCreated, jointaccountSaved)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Context) DeleteJointaccountOfUser(gc *gin.Context) {
|
||||
userId := gc.Param("userId")
|
||||
jointaccountId := gc.Param("jointaccountId")
|
||||
c.service.DeleteJointaccountOfUser(userId, jointaccountId)
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) GetSpecificJointaccountOfUser(gc *gin.Context) {
|
||||
userId := gc.Param("userId")
|
||||
jointaccountId := gc.Param("jointaccountId")
|
||||
jointaccount, _ := c.service.GetASpecificJointaccountForUser(userId, jointaccountId)
|
||||
utils.JSON(gc.Writer, http.StatusOK, jointaccount)
|
||||
}
|
||||
|
||||
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/jointaccount/model.go
Normal file
16
internal/jointaccount/model.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package jointaccount
|
||||
|
||||
import "time"
|
||||
|
||||
type Jointaccount struct {
|
||||
JointaccountEditable
|
||||
UserId string `json:"userId,omitempty"`
|
||||
}
|
||||
|
||||
type JointaccountEditable struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Provider string `json:"provider"`
|
||||
CreatedAt time.Time `json:"createdAt,omitempty"`
|
||||
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
|
||||
}
|
||||
62
internal/jointaccount/service.go
Normal file
62
internal/jointaccount/service.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package jointaccount
|
||||
|
||||
import (
|
||||
"nos-comptes/internal/storage/dao"
|
||||
"nos-comptes/internal/storage/model"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *Database
|
||||
}
|
||||
|
||||
func (s *Service) GetAllJointaccountOfUser(userId string) ([]*Jointaccount, error) {
|
||||
jointaccounts, err := s.db.GetAllJointaccountOfUser(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 jointaccounts == nil {
|
||||
return nil, &model.ErrNotFound
|
||||
}
|
||||
return jointaccounts, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetJointaccountWithNameForUser(name string, id string) (*Jointaccount, error) {
|
||||
jointaccount, err := s.db.GetJointaccountWithNameForUser(name, id)
|
||||
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
|
||||
}
|
||||
return jointaccount, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateJointaccount(jointaccount Jointaccount) (*Jointaccount, error) {
|
||||
err := s.db.CreateJointaccount(&jointaccount)
|
||||
return &jointaccount, err
|
||||
}
|
||||
|
||||
func (s *Service) DeleteJointaccountOfUser(userId, jointaccountId string) error {
|
||||
return s.db.DeleteJointaccountOfAnUser(userId, jointaccountId)
|
||||
}
|
||||
|
||||
func (s *Service) GetASpecificJointaccountForUser(userId, jointaccountId string) (*Jointaccount, error) {
|
||||
return s.db.GetASpecificJointaccountForUser(userId, jointaccountId)
|
||||
|
||||
}
|
||||
|
||||
func NewService(database *Database) *Service {
|
||||
return &Service{db: database}
|
||||
}
|
||||
Reference in New Issue
Block a user