public async Task <ActionResult <PetDTO> > AddPetAsync(PetDTO pet) { // Log Api call. Could be moved to database for future anayltics. _log.WriteInformation("Controller:PetsController,API:AddPet,DateTime:" + DateTime.Now.ToString()); try { var petEntity = _mapper.Map <Entities.Pet>(pet); // Map to entity. await _familyDemoAPIv2Repository.AddPet(petEntity); // Add. var petToReturn = _mapper.Map <PetDTO>(petEntity); // Log addition of new pet. _log.WriteInformation("New pet added", null, petToReturn); // Return person added. //return Ok(petToReturn); // Return link in header. return(CreatedAtRoute("AddPetAsync", new { petToReturn.PetId }, petToReturn)); } catch (Exception ex) { _log.WriteError(ex.Message, ex.InnerException); return(NoContent()); } }
public async Task <ActionResult <PetResponseDTO> > CreatePet(PetDTO petDTO) { long ownerId = petDTO.OwnerId; var owner = await _context.Users.FindAsync(ownerId); if (owner == null) { return(NotFound()); } long animalId = petDTO.AnimalId; var animal = await _context.Animals.FindAsync(animalId); if (animal == null) { return(NotFound()); } var pet = new Pet { Name = petDTO.Name, AnimalId = animalId, Animal = animal, OwnerId = ownerId, Owner = owner }; _context.Pets.Add(pet); await _context.SaveChangesAsync(); return(CreatedAtAction( nameof(CreatePet), new { id = pet.Id }, pet.toDTO())); }
public PetDTO SavePet(PetDTO pet, PetAccomodation accomodation) { try { if (accomodation.Available == true) { accomodation.Available = false; accomodation.AccommodationStatus = AccommodationStatus.Busy; Pet petCreate = new Pet() { AccomodationId = pet.AccomodationId, Name = pet.Name, PetAccomodation = accomodation, PetHealth = pet.PetHealth, PetOwner = pet.PetOwner, PetPhotograph = pet.PetPhotograph }; var newPet = _context.Add(petCreate).Entity; _context.SaveChanges(); return(pet); } else { return(null); } } catch (Exception ex) { throw new Exception(ex.Message); } }
public void Post_SavePet_Return_CreatedResult() { var controller = new PetController(_petServices, _accomodationServices); var pet = new PetDTO() { AccomodationId = 10, Name = "Dog Test", PetAccomodation = new Domain.PetAccomodation() { Available = true, Name = "Accomodation one", AccommodationStatus = AccommodationStatus.Free }, PetHealth = PetHealth.InTreatment, PetOwner = new PetOwner() { Name = "Owner", Address = "test street", Description = "health my pet", Phone = "855566666", }, PetPhotograph = "path photograph" }; var data = controller.SavePet(pet); Assert.IsType <CreatedResult>(data); }
public int Update(PetDTO dto) { var entity = ModelMapper.Mapper.Map <Pet>(dto); entity.EntityState = EntityState.Modified; return(_petRepository.Save(entity)); }
// Mappers // Note: doesn't expose behavior public static PetResponse FromPetDTO(PetDTO item) => new PetResponse() { Id = item.Id, Animal = AnimalResponse.FromAnimalDTO(item.Animal), Name = item.Name, Owner = EmployeeResponse.FromEmployeeDTO(item.Employee) };
public async void CreatePet(PetDTO petDTO) { var jsonString = JsonSerializer.Serialize(petDTO); var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json"); using var response = await httpClient.PutAsync("https://localhost:44336/api/CreatePet", httpContent); response.EnsureSuccessStatusCode(); }
public void CreatePet(PetDTO petDTO) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <PetDTO, Pet>()).CreateMapper(); Pet pet = mapper.Map <Pet>(petDTO); pet.Id = petIdCounter; petIdCounter++; Database.Pets.Create(pet); Database.Save(); }
public IActionResult GetPetById(int id) { PetDTO pet = _petServices.GetPetById(id); if (pet == null) { return(NotFound()); } return(Ok(pet)); }
public IActionResult SavePet([FromBody] PetDTO pet) { PetAccomodation accomodation = _accomodationServices.GetAccomodationById(pet.AccomodationId); PetDTO petCreate = _petServices.SavePet(pet, accomodation); if (petCreate == null) { return(NotFound()); } return(Created($"/api/Pet/{petCreate}", petCreate)); }
public IHttpActionResult GetPet(int id) { PetDTO petDTO = new PetDTO(_petService.GetPet(id)); if (petDTO == null) { return(NotFound()); } return(Ok(petDTO)); }
public List <PetDTO> SavePet(PetDTO PetDTO) { var entity = mapper.Map <Pet>(PetDTO); if (ownerRepository.SavePet(entity) > 0) { var data = ownerRepository.GetById(PetDTO.OwnerId); return(mapper.Map <List <PetDTO> >(data.Pets)); } return(null); }
public PetDTO GetPetById(int id) { try { Pet pet = _context.Set <Pet>().Include(p => p.PetOwner).Include(p => p.PetAccomodation).FirstOrDefault(p => p.Id == id); return(PetDTO.generatePetById(pet)); } catch (Exception ex) { throw new Exception("não foi possivel localizar"); } }
public List <PetDTO> ListAllPets() { try { var list = _context.Set <Pet>().Include(p => p.PetOwner).ToList(); var pets = PetDTO.generatePetDTO(list); return(pets); }catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task UpdatePet(PetDTO pet) { try { PetAccomodation actuallyAccomodation = _context.PetAccomodations.FirstOrDefault(p => p.PetAccomodationId == pet.AccomodationId); PetAccomodation accomodation = _context.PetAccomodations.FirstOrDefault(p => p.PetAccomodationId == pet.AccomodationId); if (accomodation.Available || pet.AccomodationId == actuallyAccomodation.PetAccomodationId) { actuallyAccomodation.Available = true; _context.PetAccomodations.Update(actuallyAccomodation); _context.SaveChanges(); _context.PetsOwner.Update(pet.PetOwner); _context.SaveChanges(); Pet newPet = new Pet() { AccomodationId = pet.AccomodationId == 0 ? accomodation.PetAccomodationId : actuallyAccomodation.PetAccomodationId, Id = pet.Id, Name = pet.Name, PetHealth = pet.PetHealth, PetPhotograph = pet.PetPhotograph }; accomodation.Available = false; if (pet == null) { throw new Exception("Pet não encontrado"); } _context.Entry <Pet>(newPet).State = EntityState.Modified; _context.Entry <PetAccomodation>(accomodation).State = EntityState.Modified; await _context.SaveChangesAsync(); } else { throw new Exception("Alojamento ocupado, favor escolher outro"); } } catch (Exception ex) { throw new Exception("Ocorreu um erro ao tentar deletar"); } }
public override void Show() { base.Show(); try { // מציגים את רשימת המשחקים Task <List <ActionOptionDTO> > list = UIMain.api.GetAllFoodAsync(); ObjectsList foods = new ObjectsList("foods", list.Result.ToList <object>()); foods.Show(); Console.WriteLine(); Console.WriteLine("Please enter food option ID:"); int foodId = int.Parse(Console.ReadLine()); const int DEAD_STATUS_ID = 4; Task <List <PetDTO> > playerPets = UIMain.api.GetPlayerPetsAsync(); PetDTO p = playerPets.Result.Where(p => p.LifeStatusId != DEAD_STATUS_ID).FirstOrDefault(); ActionOptionDTO actionOptionDTO = list.Result.Where(a => a.OptionId == foodId).FirstOrDefault(); if (p == null) { Console.WriteLine("There is no active pet"); } else { Task <bool> task = UIMain.api.DoActionFeedAsync(actionOptionDTO); if (task.Result) { Console.WriteLine($"The pet ate {actionOptionDTO.OptioName}"); } else { Console.WriteLine("Something wrong happened!"); } } } catch (Exception e) { Console.Clear(); Console.WriteLine("Something wrong happened!"); Console.WriteLine($"Error message: {e.Message}"); } Console.WriteLine("Please press any key to get back to menu!"); Console.ReadKey(true); }
public async Task <IActionResult> AddPetToUser(long id, PetDTO petDTO) { if (id != petDTO.OwnerId) { return(BadRequest()); } var user = await _context.Users.FindAsync(id); if (user == null) { return(NotFound()); } var animal = await _context.Animals.FindAsync(petDTO.AnimalId); if (animal == null) { return(NotFound()); } var pet = new Pet { Name = petDTO.Name, AnimalId = petDTO.AnimalId, Animal = animal, OwnerId = petDTO.OwnerId, Owner = user }; user.pets.Add(pet); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!UserExists(id)) { return(NotFound()); } return(CreatedAtAction( nameof(AddPetToUser), new { id = user.Id }, user.toDto())); }
public IActionResult CreatePet(String name, String type, int price) { try { PetDTO petDTO = new PetDTO { Name = name, Type = type, Price = price }; service.CreatePet(petDTO); return(Content("Животное добавлено")); } catch (Exception ex) { return(Content(ex.Message)); } }
public ActionResult <PetDTO> GetPets(int id) { Pet pet; PetDTO petDTO; try { pet = _petService.SearchById(id); if (pet.PreviousOwnerID != 0) { petDTO = new PetDTO(pet.ID, pet.Name, _petTypeService.SearchById(pet.PetTypeID), pet.BirthDate, pet.SoldDate, pet.Color, _ownerService.SearchById(pet.PreviousOwnerID), pet.Price); } else if (pet.PreviousOwnerID == 0) { petDTO = new PetDTO(pet.ID, pet.Name, _petTypeService.SearchById(pet.PetTypeID), pet.BirthDate, pet.SoldDate, pet.Color, null, pet.Price); } else { throw new DataBaseException("Something went very wrong"); } return(Ok(petDTO)); } catch (InvalidDataException e) { return(BadRequest("Something went wrong with your request\n" + e.Message)); } catch (KeyNotFoundException e) { return(NotFound("Could not find requested pet\n" + e.Message)); } catch (DataBaseException e) { return(StatusCode(500, e.Message)); } }
public int Create(PetDTO dto) { return(_petRepository.Create(dto)); }
public void CreatePet(PetDTO petDTO) { service.CreatePet(petDTO); }
public void WriteInformation(string message, AddPersonDTO personToReturn = null, PetDTO petToReturn = null) { _logger.LogInformation("Message:" + message); switch (message) { case "New pet added": _logger.LogInformation("Pet:" + petToReturn.PetId + "," + petToReturn.Name + "," + petToReturn.NickName + "," + petToReturn.PetTypeId + "," + petToReturn.PersonId + "," + petToReturn.CreateDate); break; case "New person added": _logger.LogInformation("Person:" + personToReturn.PersonId + "," + personToReturn.FirstName + "," + personToReturn.MIddleName + "," + personToReturn.LastName + "," + personToReturn.Age + "," + personToReturn.City + "," + personToReturn.DateOfBirth); break; default: break; } }
public async Task <IActionResult> UpdatePet(PetDTO pet) { await _petServices.UpdatePet(pet); return(NoContent()); }
public void AddPet(PetDTO pet) { _unitOfWork.PetRepository.Create(Mapper.Map <Pet>(pet)); _unitOfWork.Save(); }
public ActionResult PetCreate([FromForm] PetCO request) { var sonuc = new ResultDTO(); if (request == null) { throw new PetClinicAppointmentBadRequestException("You have not sent any data!"); } if (string.IsNullOrEmpty(request.Name)) { throw new PetClinicAppointmentBadRequestException("Pet name cannot be empty!"); } if (string.IsNullOrEmpty(request.PlaceOfBirth)) { throw new PetClinicAppointmentBadRequestException("Pet place of birth cannot be empty!"); } if (string.IsNullOrEmpty(request.Birthdate.ToLongDateString())) { throw new PetClinicAppointmentBadRequestException("Pet birthdate cannot be empty!"); } if (request.UserGuid == null) { throw new PetClinicAppointmentBadRequestException("User guid cannot be empty!"); } var user = _userService.GetByGuid(request.UserGuid); if (user == null) { throw new PetClinicAppointmentNotFoundException("User not found!"); } var picturePath = Path.Combine("Assets", "defaultPet.jpeg"); var dto = new PetDTO() { Guid = Guid.NewGuid(), Deleted = false, Actived = true, CreatedDate = DateTime.Now, DogumTarihi = request.Birthdate, DogumYeri = request.PlaceOfBirth, Name = request.Name, User = null, UserId = user.Id, Resim = picturePath }; var durum = _petService.Create(dto); if (durum > 0) { sonuc.Status = EDurum.SUCCESS; sonuc.Message.Add(new MessageDTO() { Code = HttpStatusCode.OK, Status = EDurum.SUCCESS, Description = "Pet was created successfully." }); sonuc.Data = new { pet = new { dto.Guid } }; } else { throw new PetClinicAppointmentBadRequestException("Error adding pet!"); } return(Ok(sonuc)); }
public IActionResult Post(PetDTO PetDTO) { List <PetDTO> result = ownerService.SavePet(PetDTO); return(Ok(result)); }
public static void ClearObjectValues(string objectType, PersonDTO personDTO = null, PetDTO petDTO = null) { switch (objectType) { case "personDTO": personDTO.FirstName = string.Empty; personDTO.LastName = string.Empty; personDTO.MIddleName = string.Empty; personDTO.Gender = string.Empty; personDTO.Age = 0; personDTO.Country = string.Empty; personDTO.StateId = 0; personDTO.state = string.Empty; personDTO.DateOfBirth = DateTime.Now; personDTO.City = string.Empty; break; case "petDTO": petDTO.Name = string.Empty; petDTO.NickName = string.Empty; petDTO.petType = string.Empty; break; default: break; } }
public PetDTO GetPet(int?id) { PetDTO pet = service.GetPet(id); return(pet); }
public int Update(PetDTO dto) { return(_petRepository.Update(dto)); }
// POST: api/Owner public void Post([FromBody] PetDTO pet) { _petService.AddPet(pet); }