public async Task <CommandResult> Handle(Command command, CancellationToken cancellationToken) { var breedId = _petShelterDbContext.Breeds.FirstOrDefault(x => x.Id == command.BreedId)?.Id; if (!breedId.HasValue) { return(new CommandResult(HttpStatusCode.BadRequest, "Invalid breed id.")); } var image = await _imageSaver.SaveImage(command.Image.OpenReadStream()); if (!_user.ShelterId.HasValue) { return(new CommandResult(HttpStatusCode.Forbidden, "User has no shelter claim.")); } var petDomain = new Pet() { Color = command.Color, Description = command.Description, Gender = command.Gender, Name = command.Name, Image = image, BreedId = breedId.Value, HandlerId = _user.Id, ShelterId = _user.ShelterId.Value }; _petShelterDbContext.Pets.Add(petDomain); _petShelterDbContext.SaveChanges(); return(new CommandResult(HttpStatusCode.Created, "Pet has been created succesfully.", petDomain.Id)); }
protected override CommandResult Handle(Command command) { var pet = _petShelterDbContext.Pets.FirstOrDefault(x => x.Id == command.PetId); if (pet == null) { return(new CommandResult(HttpStatusCode.NotFound)); } if (command.Date < DateTime.UtcNow) { return(new CommandResult( HttpStatusCode.UnprocessableEntity, "The requested datetime is in the past.")); } var bookedAppointments = _petShelterDbContext.Appointments .Where(x => x.Start.Date > DateTime.UtcNow && (x.Pet.HandlerId == pet.HandlerId || x.UserId == _userProfile.Id)) .ToList(); // commented for testing purposes //if (bookedAppointments.Any(x => x.PetId == pet.Id && x.UserId == _userProfile.Id)) //{ // return new CommandResult( // HttpStatusCode.UnprocessableEntity, // "User already has appointment with pet."); //} if (Overlap( command.Date.LocalDateTime.TimeOfDay, command.Date.LocalDateTime.Add(_apptDuration).TimeOfDay, bookedAppointments.Where(x => x.Start.LocalDateTime.Date == command.Date.LocalDateTime.Date))) { return(new CommandResult( HttpStatusCode.UnprocessableEntity, "The requested datetime overlaps with another appointment.")); } if (command.Date.LocalDateTime.TimeOfDay < _localMinTime || command.Date.LocalDateTime.Add(_apptDuration).TimeOfDay > _localMaxTime) { return(new CommandResult( HttpStatusCode.UnprocessableEntity, "The requested datetime is outside business hours.")); } var appointment = _mapper.Map <Command, Appointment>(command); appointment.UserId = _userProfile.Id; _petShelterDbContext.Appointments.Add(appointment); _petShelterDbContext.SaveChanges(); return(new CommandResult( HttpStatusCode.OK, "The appointment has been created succesfully.", appointment.Id)); }