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 printResult(w http.ResponseWriter, status int, message string, valueType interface{}) { w.WriteHeader(status) if isStatusNotOK(w, status, message) { routes.ServeJson(w, service.GetErrorMessageType(status, message)) return } routes.ServeJson(w, service.GetGeneralMsgType(status, message, valueType)) }
func resultSelectUserSQL(w http.ResponseWriter, sequel string) { rows, err := service.ExecuteChannelSqlRows(sequel) internalServerStatus := http.StatusInternalServerError if isErrNotNil(w, internalServerStatus, err) { w.WriteHeader(internalServerStatus) routes.ServeJson(w, service.GetErrorMessageType(internalServerStatus, err.Error())) return } select { case resChanUsers := <-mapUsers(rows): if resChanUsers.Datas == nil { betterEmptyThanNil := make([]interface{}, 0) resChanUsers.Datas = betterEmptyThanNil } statusOK := http.StatusOK w.WriteHeader(statusOK) routes.ServeJson(w, service.GetGeneralMsgType(statusOK, "success", resChanUsers)) case <-service.TimeOutInMilis(service.GlobalTimeOutDB): printDefaultMessage(w, 508, "request timeout") } }
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)) }