コード例 #1
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
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))
}
コード例 #2
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
func isStatusNotOK(w http.ResponseWriter, status int, message string) bool {
	if status != http.StatusOK {
		w.WriteHeader(status)
		routes.ServeJson(w, service.GetErrorMessageType(status, message))
		return true
	}
	return false
}
コード例 #3
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
func isErrNotNil(w http.ResponseWriter, status int, err error) bool {
	if err != nil {
		w.WriteHeader(status)
		routes.ServeJson(w, service.GetErrorMessageType(status, err.Error()))
		return true
	}
	return false
}
コード例 #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 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))
	}
}
コード例 #6
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)
	}
}
コード例 #7
0
ファイル: UserController.go プロジェクト: haluanice/koneksee
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")
	}
}