Пример #1
0
func CreateUser(w http.ResponseWriter, r *http.Request) {
	if r.Header.Get("Authorization") != "1234" {
		w.WriteHeader(500)
		return
	}
	NewUser := atomicUser(newUserJson(r.Body))
	if NewUser.PhoneNumber == "" {
		w.WriteHeader(422)
		routes.ServeJson(w, service.GetErrorMessageType(422, "phone_number is empty"))
		return
	}

	mobileBytes := []byte(NewUser.PhoneNumber)
	hashedPassword, err := bcrypt.GenerateFromPassword(mobileBytes, 10)

	if isErrNotNil(w, 508, err) {
		return
	}

	SQL := fmt.Sprintf("INSERT INTO users SET user_name='%s', phone_number='%s', token = '%s'", NewUser.UserName, NewUser.PhoneNumber, hashedPassword)
	status, message, newId := service.ExecuteInsertSqlResult(SQL)
	userCreated := responses.UserCreated{int(newId), NewUser.UserName, NewUser.PhoneNumber, fmt.Sprintf("%s", hashedPassword)}
	switch {
	case status == http.StatusConflict:
		// 1. Update user_name and token in users
		field := fmt.Sprintf("user_name='%s', token = '%s'", NewUser.UserName, hashedPassword)
		condition := fmt.Sprintf("phone_number = '%s'", NewUser.PhoneNumber)
		statusUpdate, messageUpdate := service.UpdateQuery("users", field, condition)
		if isStatusNotOK(w, statusUpdate, messageUpdate) {
			return
		}
		// 2. Get user_id
		conditionSelect := fmt.Sprintf("phone_number = %s", NewUser.PhoneNumber)
		sequelSelect := service.SelectQuery("user_id", "users", conditionSelect)
		sqlRow, err := service.ExecuteChannelSqlRow(sequelSelect)
		if isErrNotNil(w, 508, err) {
			return
		}
		// 3. Check if result exists
		errSqlRow := sqlRow.Scan(&userCreated.UserId)
		statusRow, messageRow := service.CheckScanRowSQL(errSqlRow)
		if isStatusNotOK(w, statusRow, messageRow) {
			return
		}
		// 4. Return existing mobile_phone with given user_name and new token
		w.WriteHeader(statusRow)
		routes.ServeJson(w, service.GetGeneralMsgType(statusRow, messageRow, userCreated))
	default:
		w.WriteHeader(status)
		routes.ServeJson(w, service.GetGeneralMsgType(status, message, userCreated))
	}
}
Пример #2
0
func GetUser(w http.ResponseWriter, r *http.Request) {
	urlParams := r.URL.Query()
	id := urlParams.Get(":id")
	user := atomicUser(requests.User{})

	condition := fmt.Sprintf("user_id = %s", id)
	sequel := service.SelectQuery("user_id, user_name, phone_number, profile_picture", "users", condition)
	sqlRow, err := service.ExecuteChannelSqlRow(sequel)
	if isErrNotNil(w, 508, err) {
		return
	}
	errSqlRow := sqlRow.Scan(&user.UserId, &user.UserName, &user.PhoneNumber, &user.ProfilePicture)
	statusRow, messageRow := service.CheckScanRowSQL(errSqlRow)
	printResult(w, statusRow, messageRow, user)
}
Пример #3
0
func getUserIdSQL(phoneNumber string) string {
	condition := fmt.Sprintf(" phone_number = '%s'", phoneNumber)
	return service.SelectQuery("user_id", "users", condition)
}
Пример #4
0
func selectUserSQL(condition string) string {
	return service.SelectQuery("user_id, user_name, phone_number, profile_picture", "users", condition)
}