-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
321 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package entity | ||
|
||
import "time" | ||
|
||
type UserID int64 | ||
|
||
type User struct { | ||
ID UserID `json:"id" db:"id"` | ||
Name string `json:"name" db:"name"` | ||
Password string `json:"password" db:"password"` | ||
Role string `json:"role" db:"role"` | ||
Created time.Time `json:"created" db:"created"` | ||
Modified time.Time `json:"modified" db:"modified"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/go-playground/validator/v10" | ||
"github.com/myeunee/GolangStudy/chapter19/section75/entity" | ||
) | ||
|
||
type RegisterUser struct { | ||
Service RegisterUserService | ||
Validator *validator.Validate | ||
} | ||
|
||
func (ru *RegisterUser) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
var b struct { | ||
Name string `json:"name" validate:"required"` | ||
Password string `json:"password" validate:"required"` | ||
Role string `json:"role" validate:"required"` | ||
} | ||
if err := json.NewDecoder(r.Body).Decode(&b); err != nil { | ||
RespondJSON(ctx, w, &ErrResponse{ | ||
Message: err.Error(), | ||
}, http.StatusInternalServerError) | ||
return | ||
} | ||
if err := ru.Validator.Struct(b); err != nil { | ||
RespondJSON(ctx, w, &ErrResponse{ | ||
Message: err.Error(), | ||
}, http.StatusBadRequest) | ||
return | ||
} | ||
u, err := ru.Service.RegisterUser(ctx, b.Name, b.Password, b.Role) | ||
if err != nil { | ||
RespondJSON(ctx, w, &ErrResponse{ | ||
Message: err.Error(), | ||
}, http.StatusInternalServerError) | ||
return | ||
} | ||
rsp := struct { | ||
ID entity.UserID `json:"id"` | ||
}{ID: u.ID} | ||
RespondJSON(ctx, w, rsp, http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/myeunee/GolangStudy/chapter19/section75/entity" | ||
"github.com/myeunee/GolangStudy/chapter19/section75/store" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type RegisterUser struct { | ||
DB store.Execer | ||
Repo UserRegister | ||
} | ||
|
||
func (r *RegisterUser) RegisterUser( | ||
ctx context.Context, name, password, role string, | ||
) (*entity.User, error) { | ||
pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot hash password: %w", err) | ||
} | ||
u := &entity.User{ | ||
Name: name, | ||
Password: string(pw), | ||
Role: role, | ||
} | ||
if err := r.Repo.RegisterUser(ctx, r.DB, u); err != nil { | ||
return nil, fmt.Errorf("failed to register: %w", err) | ||
} | ||
return u, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.