feat (user): adjust for auth, hide pass
This commit is contained in:
@@ -7,9 +7,9 @@ import (
|
||||
)
|
||||
|
||||
type Base struct {
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"type:timestamptz"`
|
||||
UpdatedAt string `json:"updatedAt" gorm:"type:timestamptz"`
|
||||
DeteledAt gorm.DeletedAt `json:"deletedAt,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"column:CreatedAt;type:timestamptz"`
|
||||
UpdatedAt time.Time `json:"updatedAt" gorm:"column:UpdatedAt;type:timestamptz"`
|
||||
DeletedAt gorm.DeletedAt `json:"deletedAt,omitempty" gorm:"column:DeletedAt"`
|
||||
}
|
||||
|
||||
type Main struct {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package user
|
||||
|
||||
import erc "simrs-vx/internal/domain/references/common"
|
||||
import (
|
||||
ecore "simrs-vx/internal/domain/base-entities/core"
|
||||
erc "simrs-vx/internal/domain/references/common"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreateDto struct {
|
||||
Name string `json:"name"`
|
||||
@@ -22,12 +26,12 @@ type ReadDetailDto struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Updatedto struct {
|
||||
type UpdateDto struct {
|
||||
Id uint `json:"id"`
|
||||
CreateDto
|
||||
}
|
||||
|
||||
type Deletedto struct {
|
||||
type DeleteDto struct {
|
||||
Id uint `json:"id"`
|
||||
}
|
||||
|
||||
@@ -36,3 +40,38 @@ type MetaDto struct {
|
||||
PageSize int `json:"page_size"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type LoginDto struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
Duration uint32 `json:"duration"` // in minutes
|
||||
}
|
||||
|
||||
type ResponseDto struct {
|
||||
ecore.Main
|
||||
Name string `json:"name"`
|
||||
Status_Code erc.StatusCode `json:"status_code"`
|
||||
FailedLoginCount uint8 `json:"failedLoginCount"`
|
||||
LastSuccessLogin *time.Time `json:"lastSuccessLogin,omitempty"`
|
||||
LastAllowdLogin *time.Time `json:"lastAllowdLogin,omitempty"`
|
||||
}
|
||||
|
||||
func (u User) ToResponse() ResponseDto {
|
||||
resp := ResponseDto{
|
||||
Name: u.Name,
|
||||
Status_Code: u.Status_Code,
|
||||
FailedLoginCount: u.FailedLoginCount,
|
||||
LastSuccessLogin: u.LastSuccessLogin,
|
||||
LastAllowdLogin: u.LastAllowdLogin,
|
||||
}
|
||||
resp.Main = u.Main
|
||||
return resp
|
||||
}
|
||||
|
||||
func ToResponseList(users []User) []ResponseDto {
|
||||
resp := make([]ResponseDto, len(users))
|
||||
for i, u := range users {
|
||||
resp[i] = u.ToResponse()
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
@@ -3,12 +3,16 @@ package user
|
||||
import (
|
||||
ecore "simrs-vx/internal/domain/base-entities/core"
|
||||
erc "simrs-vx/internal/domain/references/common"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ecore.Main // adjust this according to the needs
|
||||
Name string `json:"name" gorm:"not null;size:25"`
|
||||
Password string `json:"password" gorm:"not null;size:255"`
|
||||
Status_Code erc.StatusCode `json:"status_code" gorm:"not null;size:10"`
|
||||
FailedLoginCount uint8 `json:"failedLoginCount" gorm:"type:smallint"`
|
||||
ecore.Main // adjust this according to the needs
|
||||
Name string `json:"name" gorm:"not null;size:25"`
|
||||
Password string `json:"password" gorm:"not null;size:255"`
|
||||
Status_Code erc.StatusCode `json:"status_code" gorm:"not null;size:10"`
|
||||
FailedLoginCount uint8 `json:"failedLoginCount" gorm:"type:smallint"`
|
||||
LoginAttemptCount int `json:"-"`
|
||||
LastSuccessLogin *time.Time `json:"lastSuccessLogin,omitempty"`
|
||||
LastAllowdLogin *time.Time `json:"lastAllowdLogin,omitempty"`
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
SCActive StatusCode = "active"
|
||||
SCInactive StatusCode = "inactive"
|
||||
SCNew StatusCode = "new"
|
||||
SCActive StatusCode = "active"
|
||||
SCBlocked StatusCode = "blocked"
|
||||
SCSuspended StatusCode = "suspended"
|
||||
)
|
||||
|
||||
@@ -3,6 +3,10 @@ package handler
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
/******************** main / transaction ********************/
|
||||
auth "simrs-vx/internal/interface/main-handler/authentication"
|
||||
user "simrs-vx/internal/interface/main-handler/user"
|
||||
|
||||
/******************** external ********************/
|
||||
a "github.com/karincake/apem"
|
||||
|
||||
@@ -11,6 +15,8 @@ import (
|
||||
ssdb "simrs-vx/internal/infra/ss-db"
|
||||
|
||||
/******************** pkg ********************/
|
||||
cmw "simrs-vx/pkg/cors-manager-mw"
|
||||
hc "simrs-vx/pkg/handler-crud-helper"
|
||||
handlerlogger "simrs-vx/pkg/middleware/handler-logger"
|
||||
|
||||
///// Internal
|
||||
@@ -28,6 +34,11 @@ func SetRoutes() http.Handler {
|
||||
/******************** Main ********************/
|
||||
r.HandleFunc("/", home.Home)
|
||||
|
||||
r.HandleFunc("POST /v1/authentication/login", auth.Login)
|
||||
r.HandleFunc("POST /v1/authentication/logout", auth.Logout)
|
||||
|
||||
hc.RegCrud(r, "/v1/user", user.O)
|
||||
|
||||
/////
|
||||
return handlerlogger.SetLog(r)
|
||||
return cmw.SetCors(handlerlogger.SetLog(r))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
rw "github.com/karincake/risoles"
|
||||
sf "github.com/karincake/semprit"
|
||||
|
||||
// ua "github.com/karincake/tumpeng/auth/svc"
|
||||
|
||||
e "simrs-vx/internal/domain/main-entities/user"
|
||||
u "simrs-vx/internal/use-case/main-use-case/user"
|
||||
)
|
||||
|
||||
type myBase struct{}
|
||||
|
||||
var O myBase
|
||||
|
||||
func (obj myBase) Create(w http.ResponseWriter, r *http.Request) {
|
||||
dto := e.CreateDto{}
|
||||
if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
|
||||
return
|
||||
}
|
||||
res, err := u.Create(dto)
|
||||
rw.DataResponse(w, res, err)
|
||||
}
|
||||
|
||||
func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) {
|
||||
dto := e.ReadListDto{}
|
||||
sf.UrlQueryParam(&dto, *r.URL)
|
||||
res, err := u.ReadList(dto)
|
||||
rw.DataResponse(w, res, err)
|
||||
}
|
||||
|
||||
func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) {
|
||||
id := rw.ValidateInt(w, "id", r.PathValue("id"))
|
||||
if id <= 0 {
|
||||
return
|
||||
}
|
||||
dto := e.ReadDetailDto{}
|
||||
dto.Id = uint(id)
|
||||
res, err := u.ReadDetail(dto)
|
||||
rw.DataResponse(w, res, err)
|
||||
}
|
||||
|
||||
func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
|
||||
id := rw.ValidateInt(w, "id", r.PathValue("id"))
|
||||
if id <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
dto := e.UpdateDto{}
|
||||
if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
|
||||
return
|
||||
}
|
||||
dto.Id = uint(id)
|
||||
res, err := u.Update(dto)
|
||||
rw.DataResponse(w, res, err)
|
||||
}
|
||||
|
||||
func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
id := rw.ValidateInt(w, "id", r.PathValue("id"))
|
||||
if id <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
dto := e.DeleteDto{}
|
||||
dto.Id = uint(id)
|
||||
res, err := u.Delete(dto)
|
||||
rw.DataResponse(w, res, err)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package authentication
|
||||
|
||||
import (
|
||||
dg "github.com/karincake/apem/db-gorm-mysql"
|
||||
)
|
||||
|
||||
// just return the error code
|
||||
func GetAndCheck(input, condition any) (eCode string) {
|
||||
result := dg.I.Where(condition).Find(input)
|
||||
if result.Error != nil {
|
||||
return "fetch-fail"
|
||||
} else if result.RowsAffected == 0 {
|
||||
return "auth-login-incorrect"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetDocName(id uint) string {
|
||||
return "authentication"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package authentication
|
||||
|
||||
type TokenType string
|
||||
|
||||
const AccessToken = "Access"
|
||||
const RefreshToken = "Refresh"
|
||||
|
||||
type AuthInfo struct {
|
||||
Uuid string
|
||||
User_Id int
|
||||
User_Name string
|
||||
// User_Email string
|
||||
// User_Ref_Id int
|
||||
// User_Position_Code string
|
||||
}
|
||||
|
||||
type AuthCfg struct {
|
||||
AtSecretKey string `yaml:"atSecretKey"`
|
||||
RtSecretKey string `yaml:"rtSecretKey"`
|
||||
}
|
||||
@@ -98,11 +98,11 @@ func Create(input e.CreateDto) (*d.Data, error) {
|
||||
|
||||
return &d.Data{
|
||||
Meta: d.II{
|
||||
"source": source,
|
||||
"type": "list",
|
||||
"status": "created",
|
||||
"source": source,
|
||||
"structure": "single-data",
|
||||
"status": "created",
|
||||
},
|
||||
Data: data,
|
||||
Data: data.ToResponse(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ func ReadList(input e.ReadListDto) (*d.Data, error) {
|
||||
"page_size": strconv.Itoa(metaList.PageSize),
|
||||
"record_totalCount": strconv.Itoa(metaList.Count),
|
||||
},
|
||||
Data: dataList,
|
||||
Data: e.ToResponseList(dataList),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
data, err := ReadDetailData(input, tx)
|
||||
data, err = ReadDetailData(input, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -180,11 +180,11 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) {
|
||||
"structure": "single-data",
|
||||
"status": "fetched",
|
||||
},
|
||||
Data: data,
|
||||
Data: data.ToResponse(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Update(input e.Updatedto) (*d.Data, error) {
|
||||
func Update(input e.UpdateDto) (*d.Data, error) {
|
||||
rdDto := e.ReadDetailDto{Id: input.Id}
|
||||
var data *e.User
|
||||
var err error
|
||||
@@ -224,12 +224,12 @@ func Update(input e.Updatedto) (*d.Data, error) {
|
||||
"structure": "single-data",
|
||||
"status": "updated",
|
||||
},
|
||||
Data: data,
|
||||
Data: data.ToResponse(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func Delete(input e.Deletedto) (*d.Data, error) {
|
||||
func Delete(input e.DeleteDto) (*d.Data, error) {
|
||||
rdDto := e.ReadDetailDto{Id: input.Id}
|
||||
var data *e.User
|
||||
var err error
|
||||
@@ -264,7 +264,7 @@ func Delete(input e.Deletedto) (*d.Data, error) {
|
||||
"structure": "single-data",
|
||||
"status": "deleted",
|
||||
},
|
||||
Data: data,
|
||||
Data: data.ToResponse(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -23,9 +23,9 @@ func setCreate(src e.CreateDto, dst *e.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func setUpdate(src e.Updatedto, dst *e.User) error {
|
||||
dst.Name = src.CreateDto.Name
|
||||
dst.Status_Code = src.CreateDto.Status_Code
|
||||
func setUpdate(src e.UpdateDto, dst *e.User) error {
|
||||
dst.Name = src.Name
|
||||
dst.Status_Code = src.Status_Code
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func ReadListData(input e.ReadListDto, dbx ...*gorm.DB) ([]e.User, *e.MetaDto, e
|
||||
Scopes(gh.Paginate(input, &pagination)).
|
||||
Order("CreatedAt DESC")
|
||||
|
||||
if err := tx.Find(&data).Error; err != nil {
|
||||
if err := tx.Debug().Find(&data).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, &meta, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user