Slide 12
Slide 12 text
func (s *ServiceInput) SignUpUserInputPort(ctx
context.Context) {
// データベースの処理をする
success := s.Repository.SignUpUserRepository(ctx)
// レスポンスを返す
s.OutputPort.SignUpUserOutputPort(success)
}
func (s ServiceRepo) SignUpUserRepository(ctx context.Context) bool {
// header を取得
headerName := s.r.Header.Get("name")
headerAddress := s.r.Header.Get("address")
headerPassword := s.r.Header.Get("password")
// address が以前登録されたものと一致しないか確認
query := "select count(*) from user where address = ?"
rows, err := s.conn.QueryContext(ctx, query, headerAddress)
// ユーザー登録
query2 := "INSERT INTO user (id, name, address, status, password, chat_number, token, created_at,
updated_at) VALUES (?,?,?,?,?,?,?,?,?) "
_, err = s.conn.ExecContext(ctx, query2, userID, headerName, headerAddress, "online", headerPassword, 0,
"", s.now(), s.now())
query3 := "INSERT INTO User_Profile (id,Comment ,Friend_ID ,created_at ,updated_at) VALUES (?,?,?,?,?) "
_, err = s.conn.ExecContext(ctx, query3, userID, "こんにちは! ", "test_1234", s.now(), s.now())
return true
}
func (s *ServiceOutput) GetUsersOutputPort(users
[]entity.User) {
type getUser struct {
Name string `json:"name"`
ID string `json:"ID"`
ChatNumber int `json:"chatNumber"`
}
res := struct {
User []getUser `json:"Users"`
}{}
var getUsers []getUser
for _, e := range users {
getUsers = append(getUsers, getUser{
Name: e.Name,
ID: e.ID,
ChatNumber: e.ChatNumber,
})
}
res.User = getUsers
if err := json.NewEncoder(s.w).Encode(&res); err != nil {
log.Printf("[ERROR] response encoding failed: %+v", err)
entity.WriteHTTPError(s.w,
http.StatusInternalServerError)
}
}
Usecase 層
Adapter 層
Gateway