Esempio n. 1
0
func (i *uploadController) Create(c context.Context) error {

	isValid, appd, userd, passd, rawd := utils.CheckHeaderIsValidWithBasicAuthAndRawData(c)

	if isValid == false {
		return goweb.API.RespondWithError(c, http.StatusBadRequest,
			"Bad request in POST header")
	}

	var result types.Application

	types.DatabaseConnection.Where(&types.Application{
		ApplicationId: appd}).First(&result)

	if result.ApplicationId == appd {

		uploaded := types.NewUpload(appd,
			rawd)

		if result.Username != userd {
			log.Println("Post bad username")
			return goweb.API.RespondWithError(c, http.StatusBadRequest,
				"Bad credentials")
		}

		if utils.DoesPasswordMatchHash(result.EncryptedPassword, passd) {
			log.Println("Password matches for post")

			types.DatabaseConnection.Create(&uploaded)

			/* Update this into the application */

			result.Uploads = append(result.Uploads, uploaded)

			types.DatabaseConnection.Model(&result).Updates(types.Application{
				Uploads: result.Uploads})

		} else {
			log.Println("Post bad password")
			return goweb.API.RespondWithError(c, http.StatusBadRequest,
				"Bad credentials")
		}
	} else {

		log.Println("Application not found")
		return goweb.API.RespondWithError(c, http.StatusBadRequest,
			"Application not found")
	}

	return goweb.API.RespondWithData(c, nil)
}
func (a *applicationController) DeleteMany(c context.Context) error {

	isValid, appd, userd, passd := utils.CheckHeaderIsValidWithBasicAuth(c)

	if isValid == false {
		return goweb.API.RespondWithError(c, http.StatusBadRequest,
			"Bad request in POST header")
	}

	var result types.Application

	types.DatabaseConnection.Where(&types.Application{
		ApplicationId: appd}).First(&result)

	if result.ApplicationId == appd {

		if result.Username != userd {
			log.Println("Post bad username")
			return goweb.API.RespondWithError(c, http.StatusBadRequest,
				"Bad credentials")
		}

		if utils.DoesPasswordMatchHash(result.EncryptedPassword, passd) {
			log.Println("Password matches for post")

			types.DatabaseConnection.Delete(&result)
		} else {
			log.Println("Post bad password")
			return goweb.API.RespondWithError(c, http.StatusBadRequest,
				"Bad credentials")
		}
	} else {

		log.Println("Application not found")
		return goweb.API.RespondWithError(c, http.StatusBadRequest,
			"Application not found")
	}

	return goweb.API.RespondWithData(c, nil)
}