Skip to content

Commit

Permalink
Merge pull request #765 from NUTFes/feat/kubosaka/add-is_deleted
Browse files Browse the repository at this point in the history
ユーザーに論理削除を追加、削除機能の作成
  • Loading branch information
Kubosaka authored Jun 5, 2024
2 parents 1f1b939 + 9b9bf8f commit a46b8a2
Show file tree
Hide file tree
Showing 22 changed files with 308 additions and 31 deletions.
2 changes: 1 addition & 1 deletion api/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ENV GOOS=linux
RUN go install github.com/swaggo/swag/cmd/swag@latest

# Airをインストール
RUN go install github.com/cosmtrek/air@latest
RUN go install github.com/air-verse/air@latest
CMD ["air", "-c", ".air.toml"]


34 changes: 34 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,26 @@ const docTemplate = `{
],
},
},
"/users/delete": {
"delete": {
tags: ["user"],
"description": "userの複数作成",
responses: {
"200": {
"description": "複数のuserをidで削除できる",
}
},
"parameters": [
{
"in": "body",
"name": "destroyUserIDs",
"schema":{
"$ref": "#/definitions/destroyUserIDs"
},
},
],
},
},
"/users/{id}": {
"get": {
tags: ["user"],
Expand Down Expand Up @@ -2598,6 +2618,20 @@ const docTemplate = `{
"purchaseOrderID"
},
},
"destroyUserIDs":{
"properties":{
"deleteIDs":{
"type": "array",
"items": {
"type": "number"
},
example: []
},
},
"required":{
"deleteIDs",
},
},
"year_periods":{
"properties":{
"year":{
Expand Down
19 changes: 18 additions & 1 deletion api/externals/controller/user_controller.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package controller

import (
"net/http"

"github.com/NUTFes/FinanSu/api/internals/domain"
"github.com/NUTFes/FinanSu/api/internals/usecase"
"github.com/labstack/echo/v4"
"net/http"
)

type userController struct {
Expand All @@ -16,6 +18,7 @@ type UserController interface {
CreateUser(echo.Context) error
UpdateUser(echo.Context) error
DestroyUser(echo.Context) error
DestroyMultiUsers(echo.Context) error
GetCurrentUser(echo.Context) error
}

Expand Down Expand Up @@ -77,6 +80,20 @@ func (u *userController) DestroyUser(c echo.Context) error {
return c.String(http.StatusOK, "Destroy User")
}

// Destroy
func (u *userController) DestroyMultiUsers(c echo.Context) error {
destroyUser := new(domain.DestroyUserIDs)
if err := c.Bind(destroyUser); err != nil {
return err
}
err := u.u.DestroyMultiUsers(c.Request().Context(), destroyUser.DeleteIDs)
if err != nil {
return c.String(http.StatusBadRequest,err.Error())
}
return c.String(http.StatusOK, "Destroy Users")
}


// ログインユーザーの取得
func (auth *userController) GetCurrentUser(c echo.Context) error {
// headerからトークンを取得する
Expand Down
15 changes: 2 additions & 13 deletions api/externals/repository/purchase_order_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,8 @@ func (por *purchaseOrderRepository) DeleteReport(
func (p *purchaseOrderRepository) AllUserInfoByYear(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
purchase_orders.id,
purchase_orders.deadline,
purchase_orders.user_id,
purchase_orders.expense_id,
purchase_orders.finance_check,
purchase_orders.created_at,
purchase_orders.updated_at,
users.id,
users.name,
users.bureau_id,
users.role_id,
users.created_at,
users.updated_at
purchase_orders.*,
users.*
FROM
purchase_orders
INNER JOIN
Expand Down
44 changes: 39 additions & 5 deletions api/externals/repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"context"
"database/sql"
"strconv"

"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/NUTFes/FinanSu/api/externals/repository/abstract"
Expand All @@ -19,6 +20,7 @@ type UserRepository interface {
Create(context.Context, string, string, string) error
Update(context.Context, string, string, string, string) error
Destroy(context.Context, string) error
MultiDestroy(context.Context, []int) error
FindNewRecord(context.Context) (*sql.Row, error)
FindByEmail(context.Context, string) (*sql.Row, error)
}
Expand All @@ -29,7 +31,7 @@ func NewUserRepository(c db.Client, ac abstract.Crud) UserRepository {

// 全件取得
func (ur *userRepository) All(c context.Context) (*sql.Rows, error) {
query := "SELECT * FROM users"
query := "SELECT * FROM users WHERE is_deleted IS FALSE"
return ur.crud.Read(c, query)
}

Expand Down Expand Up @@ -63,17 +65,49 @@ func (ur *userRepository) Update(c context.Context, id string, name string, bure

// 削除
func (ur *userRepository) Destroy(c context.Context, id string) error {
query := "DELETE FROM users WHERE id = " + id
return ur.crud.UpdateDB(c, query)
query := "UPDATE users SET is_deleted = TRUE WHERE id =" + id

err := ur.crud.UpdateDB(c, query)

query = "UPDATE mail_auth SET email = NULL WHERE user_id =" + id
ur.crud.UpdateDB(c, query)

return err
}

// 複数削除
func (ur *userRepository) MultiDestroy(c context.Context, ids []int) error {
query := "UPDATE users SET is_deleted = TRUE WHERE "
query2 := "UPDATE mail_auth SET email = NULL WHERE "
for index, id := range ids {
query += "id = " + strconv.Itoa(id)
query2 += "user_id = " + strconv.Itoa(id)

if(index != len(ids)-1){
query += " OR "
query2 += " OR "
}

}

err := ur.crud.UpdateDB(c, query)
if err != nil {
return err
}

err = ur.crud.UpdateDB(c, query2)

return err
}


func (ur *userRepository) FindNewRecord(c context.Context) (*sql.Row, error) {
query := "SELECT * FROM users ORDER BY id DESC LIMIT 1"
query := "SELECT * FROM users WHERE is_deleted IS FALSE ORDER BY id DESC LIMIT 1"
return ur.crud.ReadByID(c, query)
}

// 1件取得
func (ur *userRepository) FindByEmail(c context.Context, email string) (*sql.Row, error) {
query := "SELECT * FROM users INNER JOIN mail_auth ON users.id = mail_auth.user_id WHERE email = '" + email + "'"
query := "SELECT * FROM users INNER JOIN mail_auth ON users.id = mail_auth.user_id WHERE is_deleted IS FALSE AND email = '" + email + "'"
return ur.crud.ReadByID(c, query)
}
5 changes: 5 additions & 0 deletions api/internals/domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type User struct {
Name string `json:"name"`
BureauID int `json:"bureauID"`
RoleID int `json:"roleID"`
IsDeleted bool `json:"isDeleted"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Expand All @@ -17,3 +18,7 @@ type UserDetail struct{
User User
MailAuth MailAuth
}

type DestroyUserIDs struct {
DeleteIDs []int `json:"deleteIDs"`
}
2 changes: 2 additions & 0 deletions api/internals/usecase/activity_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (a *activityUseCase) GetActivityDetail(c context.Context) ([]domain.Activit
&activity.User.Name,
&activity.User.BureauID,
&activity.User.RoleID,
&activity.User.IsDeleted,
&activity.User.CreatedAt,
&activity.User.UpdatedAt,
)
Expand Down Expand Up @@ -297,6 +298,7 @@ func (a *activityUseCase) GetActivityDetailsByPeriod(c context.Context, year str
&activity.User.Name,
&activity.User.BureauID,
&activity.User.RoleID,
&activity.User.IsDeleted,
&activity.User.CreatedAt,
&activity.User.UpdatedAt,
)
Expand Down
3 changes: 3 additions & 0 deletions api/internals/usecase/fund_information_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (f *fundInformationUseCase) GetFundInformationDetails(c context.Context) ([
&fundInformationDetail.User.Name,
&fundInformationDetail.User.BureauID,
&fundInformationDetail.User.RoleID,
&fundInformationDetail.User.IsDeleted,
&fundInformationDetail.User.CreatedAt,
&fundInformationDetail.User.UpdatedAt,
&fundInformationDetail.Teacher.ID,
Expand Down Expand Up @@ -221,6 +222,7 @@ func (f *fundInformationUseCase) GetFundInformationDetailByID(c context.Context,
&fundInformationDetail.User.Name,
&fundInformationDetail.User.BureauID,
&fundInformationDetail.User.RoleID,
&fundInformationDetail.User.IsDeleted,
&fundInformationDetail.User.CreatedAt,
&fundInformationDetail.User.UpdatedAt,
&fundInformationDetail.Teacher.ID,
Expand Down Expand Up @@ -270,6 +272,7 @@ func (f *fundInformationUseCase) GetFundInformationDetailsByPeriod(c context.Con
&fundInformationDetail.User.Name,
&fundInformationDetail.User.BureauID,
&fundInformationDetail.User.RoleID,
&fundInformationDetail.User.IsDeleted,
&fundInformationDetail.User.CreatedAt,
&fundInformationDetail.User.UpdatedAt,
&fundInformationDetail.Teacher.ID,
Expand Down
1 change: 1 addition & 0 deletions api/internals/usecase/password_reset_token_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (p *passwordResetTokenUseCase) PasswordResetTokenRequest(c context.Context,
&user.Name,
&user.BureauID,
&user.RoleID,
&user.IsDeleted,
&user.CreatedAt,
&user.UpdatedAt,
&mailAuth.ID,
Expand Down
3 changes: 3 additions & 0 deletions api/internals/usecase/purchase_order_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (p *purchaseOrderUseCase) GetPurchaseOrderDetails(c context.Context) ([]dom
&orderDetail.User.Name,
&orderDetail.User.BureauID,
&orderDetail.User.RoleID,
&orderDetail.User.IsDeleted,
&orderDetail.User.CreatedAt,
&orderDetail.User.UpdatedAt,
)
Expand Down Expand Up @@ -215,6 +216,7 @@ func (p *purchaseOrderUseCase) GetPurchaseOrderDetailByID(c context.Context, id
&orderDetail.User.Name,
&orderDetail.User.BureauID,
&orderDetail.User.RoleID,
&orderDetail.User.IsDeleted,
&orderDetail.User.CreatedAt,
&orderDetail.User.UpdatedAt,
)
Expand Down Expand Up @@ -266,6 +268,7 @@ func (p *purchaseOrderUseCase) GetPurchaseOrderDetailsByYear(c context.Context,
&orderDetail.User.Name,
&orderDetail.User.BureauID,
&orderDetail.User.RoleID,
&orderDetail.User.IsDeleted,
&orderDetail.User.CreatedAt,
&orderDetail.User.UpdatedAt,
)
Expand Down
6 changes: 6 additions & 0 deletions api/internals/usecase/purchase_report_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetails(c context.Context) ([]d
&purchaseReportDetail.ReportUser.Name,
&purchaseReportDetail.ReportUser.BureauID,
&purchaseReportDetail.ReportUser.RoleID,
&purchaseReportDetail.ReportUser.IsDeleted,
&purchaseReportDetail.ReportUser.CreatedAt,
&purchaseReportDetail.ReportUser.UpdatedAt,
&purchaseReportDetail.PurchaseOrder.ID,
Expand All @@ -191,6 +192,7 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetails(c context.Context) ([]d
&purchaseReportDetail.OrderUser.Name,
&purchaseReportDetail.OrderUser.BureauID,
&purchaseReportDetail.OrderUser.RoleID,
&purchaseReportDetail.OrderUser.IsDeleted,
&purchaseReportDetail.OrderUser.CreatedAt,
&purchaseReportDetail.OrderUser.UpdatedAt,
)
Expand Down Expand Up @@ -244,6 +246,7 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetailByID(c context.Context, i
&purchaseReportDetail.ReportUser.Name,
&purchaseReportDetail.ReportUser.BureauID,
&purchaseReportDetail.ReportUser.RoleID,
&purchaseReportDetail.ReportUser.IsDeleted,
&purchaseReportDetail.ReportUser.CreatedAt,
&purchaseReportDetail.ReportUser.UpdatedAt,
&purchaseReportDetail.PurchaseOrder.ID,
Expand All @@ -257,6 +260,7 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetailByID(c context.Context, i
&purchaseReportDetail.OrderUser.Name,
&purchaseReportDetail.OrderUser.BureauID,
&purchaseReportDetail.OrderUser.RoleID,
&purchaseReportDetail.OrderUser.IsDeleted,
&purchaseReportDetail.OrderUser.CreatedAt,
&purchaseReportDetail.OrderUser.UpdatedAt,
)
Expand Down Expand Up @@ -312,6 +316,7 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetailsByYear(c context.Context
&purchaseReportDetail.ReportUser.Name,
&purchaseReportDetail.ReportUser.BureauID,
&purchaseReportDetail.ReportUser.RoleID,
&purchaseReportDetail.ReportUser.IsDeleted,
&purchaseReportDetail.ReportUser.CreatedAt,
&purchaseReportDetail.ReportUser.UpdatedAt,
&purchaseReportDetail.PurchaseOrder.ID,
Expand All @@ -325,6 +330,7 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetailsByYear(c context.Context
&purchaseReportDetail.OrderUser.Name,
&purchaseReportDetail.OrderUser.BureauID,
&purchaseReportDetail.OrderUser.RoleID,
&purchaseReportDetail.OrderUser.IsDeleted,
&purchaseReportDetail.OrderUser.CreatedAt,
&purchaseReportDetail.OrderUser.UpdatedAt,
)
Expand Down
11 changes: 11 additions & 0 deletions api/internals/usecase/user_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type UserUseCase interface {
CreateUser(context.Context, string, string, string) (domain.User, error)
UpdateUser(context.Context, string, string, string, string) (domain.User, error)
DestroyUser(context.Context, string) error
DestroyMultiUsers(context.Context, []int) error
GetCurrentUser(context.Context, string) (domain.User, error)
}

Expand All @@ -45,6 +46,7 @@ func (u *userUseCase) GetUsers(c context.Context) ([]domain.User, error) {
&user.Name,
&user.BureauID,
&user.RoleID,
&user.IsDeleted,
&user.CreatedAt,
&user.UpdatedAt,
)
Expand All @@ -67,6 +69,7 @@ func (u *userUseCase) GetUserByID(c context.Context, id string) (domain.User, er
&user.Name,
&user.BureauID,
&user.RoleID,
&user.IsDeleted,
&user.CreatedAt,
&user.UpdatedAt,
)
Expand All @@ -87,6 +90,7 @@ func (u *userUseCase) CreateUser(c context.Context, name string, bureauID string
&latastUser.Name,
&latastUser.BureauID,
&latastUser.RoleID,
&latastUser.IsDeleted,
&latastUser.CreatedAt,
&latastUser.UpdatedAt,
)
Expand All @@ -105,6 +109,7 @@ func (u *userUseCase) UpdateUser(c context.Context, id string, name string, bure
&updatedUser.Name,
&updatedUser.BureauID,
&updatedUser.RoleID,
&updatedUser.IsDeleted,
&updatedUser.CreatedAt,
&updatedUser.UpdatedAt,
)
Expand All @@ -119,6 +124,11 @@ func (u *userUseCase) DestroyUser(c context.Context, id string) error {
return err
}

func (u *userUseCase) DestroyMultiUsers(c context.Context, ids []int) error {
err := u.userRep.MultiDestroy(c, ids)
return err
}

func (u *userUseCase) GetCurrentUser(c context.Context, accessToken string) (domain.User, error) {
var session = domain.Session{}
var user = domain.User{}
Expand All @@ -145,6 +155,7 @@ func (u *userUseCase) GetCurrentUser(c context.Context, accessToken string) (dom
&user.Name,
&user.BureauID,
&user.RoleID,
&user.IsDeleted,
&user.CreatedAt,
&user.UpdatedAt,
)
Expand Down
Loading

0 comments on commit a46b8a2

Please sign in to comment.