Exemple #1
0
func (c Controller) UserCreate(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	user := &model.User{}
	if check.InputErr(w, r, user) {
		return
	}

	// Validate says which field is invalid
	if msg := user.Validate(); msg != "" {
		io.ErrClient(w, msg)
		return
	}

	// we check dublicates on insert
	user, err := c.stg.UserInsert(ctx, user)
	if err != nil {
		if c.stg.IsErrDup(err) {
			io.ErrClient(w, "The email is already registered")
			return
		}
		check.DBErr(w, err)
		return
	}

	// success
	resp := map[string]string{
		"message": "Please, check your mailbox for confirmation letter",
		"id":      user.ID,
	}
	io.Output(w, resp)
}
Exemple #2
0
// ProfileUpdate Handler allows to modify own user profile.
func (c Controller) ProfileUpdate(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	// read profile from input
	profile := &model.Profile{}
	if check.InputErr(w, r, profile) {
		return
	}

	// Validate says which field is invalid
	if msg := profile.Validate(); msg != "" {
		io.ErrClient(w, msg)
		return
	}

	// When storage updates profile it also aupdates History, so there is two model objects have to be updated.
	// In complex program we would have to implement transaction object and would used it like :
	// txn := stg.NewTransaction(ctx)
	// txn.ProfileUpdate(profile)
	// txn.HistoryUpdate(history)
	// txn.Execute()
	profile, err := c.stg.ProfileUpdate(ctx, profile)
	if c.dbErrOrEmpty(w, err, io.ERR_NOPROF) {
		return
	}

	io.Output(w, profile)
}