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

Develop #96

Merged
merged 2 commits into from
Sep 17, 2024
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
19 changes: 14 additions & 5 deletions pkg/client/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/ryo-arima/mark1/pkg/client/repository"
"github.com/ryo-arima/mark1/pkg/client/repository/templates"
"github.com/ryo-arima/mark1/pkg/client/usecase"
"github.com/ryo-arima/mark1/pkg/config"
"github.com/ryo-arima/mark1/pkg/entity/request"
Expand All @@ -18,7 +19,8 @@ func InitBootstrapUserCmdForAdminUser(conf config.BaseConfig) *cobra.Command {
Long: "bootstrap the value of a key",
Run: func(cmd *cobra.Command, args []string) {
userRepository := repository.NewUserRepository(conf)
userUsecase := usecase.NewUserUsecase(userRepository)
templateRepository := templates.NewTemplateRepository(conf)
userUsecase := usecase.NewUserUsecase(userRepository, templateRepository)
userUsecase.BootstrapUserForDB(request.UserRequest{})
},
}
Expand Down Expand Up @@ -52,7 +54,8 @@ func InitCreateUserCmdForAnonymousUser(conf config.BaseConfig) *cobra.Command {
},
}
userRepository := repository.NewUserRepository(conf)
userUsecase := usecase.NewUserUsecase(userRepository)
templateRepository := templates.NewTemplateRepository(conf)
userUsecase := usecase.NewUserUsecase(userRepository, templateRepository)
userUsecase.CreateUserForPublic(_request)
},
}
Expand Down Expand Up @@ -98,7 +101,7 @@ func InitGetUserCmdForAppUser(conf config.BaseConfig) *cobra.Command {

func InitGetUserCmdForAdminUser(conf config.BaseConfig) *cobra.Command {
getUserCmd := &cobra.Command{
Use: "user",
Use: "users",
Short: "get the value of a key",
Long: "get the value of a key",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -122,6 +125,10 @@ func InitGetUserCmdForAdminUser(conf config.BaseConfig) *cobra.Command {
if err != nil {
log.Fatal(err)
}
_isJson, err := cmd.Flags().GetBool("json")
if err != nil {
log.Fatal(err)
}
_request := request.UserRequest{
User: request.User{
ID: _id,
Expand All @@ -132,15 +139,17 @@ func InitGetUserCmdForAdminUser(conf config.BaseConfig) *cobra.Command {
},
}
userRepository := repository.NewUserRepository(conf)
userUsecase := usecase.NewUserUsecase(userRepository)
userUsecase.GetUserForPrivate(_request)
templateRepository := templates.NewTemplateRepository(conf)
userUsecase := usecase.NewUserUsecase(userRepository, templateRepository)
userUsecase.GetUserForPrivate(_request, _isJson)
},
}
getUserCmd.Flags().StringP("id", "", "", "id")
getUserCmd.Flags().StringP("uuid", "", "", "uuid")
getUserCmd.Flags().StringP("email", "", "", "email")
getUserCmd.Flags().StringP("name", "", "", "name")
getUserCmd.Flags().StringP("status", "", "", "status")
getUserCmd.Flags().BoolP("json", "", false, "json")
return getUserCmd
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/client/repository/templates/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package templates

import (
"encoding/json"
"fmt"
"log"
"os"
"text/template"

"github.com/ryo-arima/mark1/pkg/config"
"github.com/ryo-arima/mark1/pkg/entity/response"
)

type TemplateRepository interface {
OutputUsers(users []response.User)
OutputUsersJson(users []response.User)
}

type templateRepository struct {
BaseConfig config.BaseConfig
}

func (templateRepository templateRepository) OutputUsers(users []response.User) {
templateDir := templateRepository.BaseConfig.YamlConfig.Application.Client.TemplatesDir
t, err := template.ParseFiles(templateDir + "users.tpl")
if err != nil {
log.Fatalf("Error parsing template file: %v", err)
}
data := map[string]interface{}{
"Users": users,
}
err = t.Execute(os.Stdout, data)
if err != nil {
log.Fatalf("Error executing template: %v", err)
}
}

func (templateRepository templateRepository) OutputUsersJson(users []response.User) {
jsonData, err := json.MarshalIndent(users, "", " ") // インデント付きのJSON出力
if err != nil {
log.Fatalf("Error marshalling to JSON: %v", err)
}
fmt.Println(string(jsonData))
}

func NewTemplateRepository(baseConfig config.BaseConfig) TemplateRepository {
return templateRepository{BaseConfig: baseConfig}
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions pkg/client/repository/templates/users.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+----+--------------------------------------+-----------------------+-----------+-----------+---------------------+---------------------+---------------------+
| ID | UUID | Email | Name | Status | Created At | Updated At | Deleted At |
+----+--------------------------------------+-----------------------+-----------+-----------+---------------------+---------------------+---------------------+
{{range .Users}}| {{.ID}} | {{.UUID}} | {{.Email}} | {{.Name}} | {{.Status}} | {{.CreatedAt}} | {{.UpdatedAt}} | {{if .DeletedAt}}{{.DeletedAt}}{{else}} {{end}} |
+----+--------------------------------------+-----------------------+-----------+-----------+---------------------+---------------------+---------------------+
{{end}}
12 changes: 10 additions & 2 deletions pkg/client/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ func (userRepository userRepository) GetUserForInternal(request request.UserRequ
func (userRepository userRepository) GetUserForPrivate(request request.UserRequest) (response response.UserResponse) {
URL := userRepository.BaseConfig.YamlConfig.Application.Client.ServerEndpoint + "/api/private/users"

resp, err := http.Get(URL)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
return response
}

req.Header.Set("Authorization", userRepository.BaseConfig.Token)

client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return response
}
Expand All @@ -63,9 +71,9 @@ func (userRepository userRepository) GetUserForPrivate(request request.UserReque

err = json.Unmarshal(body, &response)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return response
}

return response
}

Expand Down
21 changes: 15 additions & 6 deletions pkg/client/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"

"github.com/ryo-arima/mark1/pkg/client/repository"
"github.com/ryo-arima/mark1/pkg/client/repository/templates"
"github.com/ryo-arima/mark1/pkg/entity/request"
)

type UserUsecase interface {
BootstrapUserForDB(request request.UserRequest)
GetUserForInternal(request request.UserRequest)
GetUserForPrivate(request request.UserRequest)
GetUserForPrivate(request request.UserRequest, isJson bool)
CreateUserForPublic(request request.UserRequest)
CreateUserForInternal(request request.UserRequest)
CreateUserForPrivate(request request.UserRequest)
Expand All @@ -21,7 +22,8 @@ type UserUsecase interface {
}

type userUsecase struct {
UserRepository repository.UserRepository
UserRepository repository.UserRepository
TemplateRepository templates.TemplateRepository
}

// Bootstrap
Expand All @@ -36,9 +38,13 @@ func (userUsecase userUsecase) GetUserForInternal(request request.UserRequest) {
fmt.Println(users)
}

func (userUsecase userUsecase) GetUserForPrivate(request request.UserRequest) {
func (userUsecase userUsecase) GetUserForPrivate(request request.UserRequest, isJson bool) {
users := userUsecase.UserRepository.GetUserForPrivate(request)
fmt.Println(users)
if isJson {
userUsecase.TemplateRepository.OutputUsersJson(users.Users)
} else {
userUsecase.TemplateRepository.OutputUsers(users.Users)
}
}

// CREATE
Expand Down Expand Up @@ -79,6 +85,9 @@ func (userUsecase userUsecase) DeleteUserForPrivate(request request.UserRequest)
fmt.Println(users)
}

func NewUserUsecase(userRepository repository.UserRepository) UserUsecase {
return &userUsecase{UserRepository: userRepository}
func NewUserUsecase(userRepository repository.UserRepository, templateRepository templates.TemplateRepository) UserUsecase {
return &userUsecase{
UserRepository: userRepository,
TemplateRepository: templateRepository,
}
}
1 change: 1 addition & 0 deletions pkg/config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Client struct {
UserEmail string `yaml:"UserEmail"`
UserPassword string `yaml:"UserPassword"`
HomeDir string `yaml:"HomeDir"`
TemplatesDir string `yaml:"TemplatesDir"`
}

type Application struct {
Expand Down
22 changes: 16 additions & 6 deletions pkg/server/controller/user_private.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package controller

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/ryo-arima/mark1/pkg/entity/model"
"github.com/ryo-arima/mark1/pkg/entity/request"
"github.com/ryo-arima/mark1/pkg/entity/response"
"github.com/ryo-arima/mark1/pkg/server/middleware"
"github.com/ryo-arima/mark1/pkg/server/repository"
)

Expand All @@ -22,13 +24,21 @@ type userControllerForPrivate struct {
}

func (userController userControllerForPrivate) GetUsers(c *gin.Context) {
var userRequest request.UserRequest
if err := c.Bind(&userRequest); err != nil {
c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_GET__FOR__001", Message: err.Error(), Users: []response.User{}})
return
var res response.UserResponse
users := userController.UserRepository.GetUsers()
for _, user := range users {
res.Users = append(res.Users, response.User{
ID: fmt.Sprintf("%v", user.ID),
UUID: user.UUID,
Email: user.Email,
Name: user.Name,
Status: user.Status,
CreatedAt: middleware.GetStringTime(user.CreatedAt),
UpdatedAt: middleware.GetStringTime(user.UpdatedAt),
DeletedAt: middleware.GetStringTime(user.DeletedAt),
})
}
res := userController.UserRepository.GetUsers()
c.JSON(http.StatusOK, res)
c.JSON(http.StatusOK, response.UserResponse{Code: "SERVER_CONTROLLER_GET__FOR__001", Message: "success", Users: res.Users})
return
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/server/middleware/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ func GetNowTime() *string {
formattedTime := currentTime.Format("2006-01-02 15:04:05")
return &formattedTime
}

func GetStringTime(time_p *string) string {
if time_p == nil {
return ""
} else {
return *time_p
}
}
1 change: 1 addition & 0 deletions tmp/build-errors.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit status 1