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)) } }
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) }
func GenerateNewToken(w http.ResponseWriter, r *http.Request) { userId := 0 userTokenJson := requests.PhoneNumberJson{} service.DecodeJson(&userTokenJson, r.Body) sqlRow, err := service.ExecuteChannelSqlRow(getUserIdSQL(userTokenJson.PhoneNumber)) if isErrNotNil(w, 508, err) { return } errSqlRow := sqlRow.Scan(&userId) status, message := service.CheckScanRowSQL(errSqlRow) if isStatusNotOK(w, status, message) { return } phoneNumber := userTokenJson.PhoneNumber resultHashed := hashedMobileNumber(phoneNumber) statusInsertToken, messageInsertToken := insertTokenToUsersTable(resultHashed, phoneNumber) w.WriteHeader(statusInsertToken) if isStatusNotOK(w, statusInsertToken, messageInsertToken) { return } userToken := requests.UserToken{phoneNumber, resultHashed} routes.ServeJson(w, service.GetGeneralMsgType(statusInsertToken, messageInsertToken, userToken)) }
func blockFriend(phoneNumber string, friendPhoneNumber string, status int) (int, string) { var friendUserId int sqlRow, err := service.ExecuteChannelSqlRow(getUserIdSQL(friendPhoneNumber)) switch { case err != nil: return 508, err.Error() default: errSqlRow := sqlRow.Scan(&friendUserId) statusRow, messageRow := service.CheckScanRowSQL(errSqlRow) switch { case statusRow == 404: return statusRow, "phone number doesn't exists" case statusRow != 200: return statusRow, messageRow default: sequel := fmt.Sprintf("INSERT INTO friends_relationship SET user_phone_number = '%s', friend_phone_number = '%s', status = %v", phoneNumber, friendPhoneNumber, status) statusInsert, messageInsert := service.ExecuteChannelSqlResult(sequel) return statusInsert, messageInsert } } }