Пример #1
0
        public ResponseResult ApplyForAnimal(AdopterApplyModel model)
        {
            var animal = _dbContext.Animals
                         .Include(a => a.Adopters)
                         .FirstOrDefault(a => a.Id == model.AnimalId && !a.HasBeenAdopted);

            if (animal is null)
            {
                return(ResponseResult.Error("Animal not available"));
            }

            if (animal.Adopters.Any(a => a.Email == model.Email.ToLower().Trim()))
            {
                return(ResponseResult.Error("Already applied for animal"));
            }

            var adopter = new Adopter
            {
                FirstName                = model.FirstName,
                LastName                 = model.LastName,
                Email                    = model.Email.ToLower().Trim(),
                City                     = model.City,
                Motivation               = model.Motivation,
                ConfirmationToken        = RandomGenerator.GenerateRandomString(),
                HasConfirmedMail         = false,
                HasReceivedDocumentation = false,
                AnimalId                 = model.AnimalId,
            };

            _dbContext.Adopters.Add(adopter);
            _dbContext.SaveChanges();

            var emailToAdopter = EmailConstructor.ConstructConfirmationEmail(adopter);

            _emailService.SendEmail(emailToAdopter);

            return(ResponseResult.Ok);
        }
Пример #2
0
        public ActionResult ApplyForAnimal(AdopterApplyModel model)
        {
            var result = _adopterRepository.ApplyForAnimal(model);

            return(ResponseToActionResult(result));
        }