Esempio n. 1
0
        public async Task <IActionResult> Post([FromBody] Donation donation)
        {
            var user = userRepository.GetUserFromPrincipal(HttpContext.User);

            if (!userRepository.IsActiveUser(user))
            {
                return(new UnauthorizedResult());
            }
            if (!userRepository.IsVerifiedUser(user))
            {
                return(BadRequest("Your account has not been verified yet. Please try again later."));
            }
            donation.PictureUrl = await imageService.UploadImageAsync(donation.Image);

            donation.DonorId       = user.Id;
            donation.Created       = DateTime.Now;
            donation.Updated       = DateTime.Now;
            donation.StatusUpdated = DateTime.Now;
            if (donation.PictureUrl == null)
            {
                donation.PictureUrl = "Empty";
            }
            if (donation.Expiration == null)
            {
                var expires = DateTime.Now;
                donation.Expiration = expires.AddHours(2);
            }
            var insertedDonation = donationRepository.Create(donation);

            if (insertedDonation != null)
            {
                return(Ok(insertedDonation));
            }
            else
            {
                return(BadRequest("Invalid donation data."));
            }
        }