コード例 #1
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
func UpdateUserName(w http.ResponseWriter, r *http.Request) {
	phoneNumber := r.Header.Get("phone_number")
	updateUserName := requests.UpdateUserName{}
	service.DecodeJson(&updateUserName, r.Body)
	userName := updateUserName.UserName
	table := "users"
	field := fmt.Sprintf("user_name='%s'", userName)
	condition := fmt.Sprintf("phone_number = '%s'", phoneNumber)
	statusUpdate, messageUpdate := service.UpdateQuery(table, field, condition)
	w.WriteHeader(statusUpdate)
	printResult(w, statusUpdate, messageUpdate, requests.UserUpdateType{userName, phoneNumber})
}
コード例 #2
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
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))
	}
}
コード例 #3
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
func UpdatePhoneNumber(w http.ResponseWriter, r *http.Request) {
	phoneNumber := r.Header.Get("phone_number")
	userTokenJson := requests.PhoneNumberJson{}
	service.DecodeJson(&userTokenJson, r.Body)
	newphoneNumber := userTokenJson.PhoneNumber
	if phoneNumber == "" {
		w.WriteHeader(400)
		routes.ServeJson(w, service.GetErrorMessageType(400, "data empty"))
	} else {
		resultHashed := hashedMobileNumber(phoneNumber)
		field := fmt.Sprintf("phone_number = '%s', token = '%s'", newphoneNumber, resultHashed)
		condition := fmt.Sprintf("phone_number = '%s'", phoneNumber)
		statusUpdate, messageUpdate := service.UpdateQuery("users", field, condition)
		userToken := requests.UserToken{newphoneNumber, resultHashed}
		printResult(w, statusUpdate, messageUpdate, userToken)
	}
}
コード例 #4
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
func UploadFile(w http.ResponseWriter, r *http.Request) {
	phoneNumber := r.Header.Get("phone_number")
	file, header, err := r.FormFile("file")
	statusNotAcceptable := http.StatusNotAcceptable
	// 1. Get file from form-data
	if isErrNotNil(w, statusNotAcceptable, err) {
		return
	}
	// 2. Read file
	fileType := header.Header.Get("Content-Type")

	if !allowedImageType(fileType) {
		w.WriteHeader(http.StatusUnsupportedMediaType)
		routes.ServeJson(w, service.GetErrorMessageType(http.StatusUnsupportedMediaType, "type is not allowed"))
		return
	}
	// 3. Generate new filename
	nameFile, errNewPath := service.GenerateNewPath()
	if isErrNotNil(w, statusNotAcceptable, errNewPath) {
		return
	}
	// 4. Read multipart file

	buff, errReadFile := ioutil.ReadAll(file)
	if isErrNotNil(w, statusNotAcceptable, errReadFile) {
		return
	}
	//5. Upload to cloudinary
	resChannelUpload := service.UploadImage(nameFile, buff)
	cloudinaryInfo := <-resChannelUpload
	close(resChannelUpload)
	if cloudinaryInfo.Err != nil {
		internalServerStatus := http.StatusInternalServerError
		w.WriteHeader(internalServerStatus)
		routes.ServeJson(w, service.GetErrorMessageType(internalServerStatus, "internal server error with cloudinary"))
		return
	}
	// 6. Update cloudinary path to profile user
	cloudinaryPath := cloudinaryInfo.FilePath
	field := fmt.Sprintf("profile_picture = '%s'", cloudinaryPath)
	condition := fmt.Sprintf("phone_number = '%s'", phoneNumber)
	statusUpdate, messageUpdate := service.UpdateQuery("users", field, condition)
	profilePictureUser := requests.UserProfilePictureType{phoneNumber, cloudinaryPath}
	printResult(w, statusUpdate, messageUpdate, profilePictureUser)
}
コード例 #5
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
func insertTokenToUsersTable(token string, phoneNumber string) (int, string) {
	field := fmt.Sprintf("token = '%s'", token)
	condition := fmt.Sprintf("phone_number = '%s'", phoneNumber)
	return service.UpdateQuery("users", field, condition)
}