Example #1
0
        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 async Task Add(Client client)
 {
     _context.Clients !.Add(client);
     await _context.SaveChangesAsync();
 }
Example #3
0
 public async Task Add(Pet pet)
 {
     _context.Pets.Add(pet);
     await _context.SaveChangesAsync();
 }