public Pet AddPet(PetRequest petRequest) { var breedTyp = _context.BreedTypes.Where(m => m.Name == petRequest.BreedName).FirstOrDefault(); int max = _context.BreedTypes.DefaultIfEmpty().Max(m => m == null ? 0 : m.IdBreedType); int maxP = _context.Pets.DefaultIfEmpty().Max(m => m == null ? 0 : m.IdPet); int idBreed = _context.BreedTypes.Where(m => m.Name == petRequest.BreedName).Select(m => m.IdBreedType).FirstOrDefault(); var pet = new Pet(); if (breedTyp == null) { var newBreed = new BreedType { IdBreedType = max + 1, Name = petRequest.BreedName }; _context.Attach(newBreed); _context.Add(newBreed); pet = new Pet { IdPet = maxP + 1, IdBreedType = max + 1, Name = petRequest.Name, IsMale = petRequest.IsMale, DateRegistered = petRequest.DateRegistered, DateOfBirth = petRequest.DateOfBirth }; _context.Attach(pet); _context.Add(pet); _context.SaveChanges(); } else { pet = new Pet { IdPet = maxP + 1, IdBreedType = idBreed, Name = petRequest.Name, IsMale = petRequest.IsMale, DateRegistered = petRequest.DateRegistered, DateOfBirth = petRequest.DateOfBirth }; _context.Attach(pet); _context.Add(pet); _context.SaveChanges(); } return(pet); }
public IActionResult putPlayer(Request req, int ID) { Player player = new Player { IdPlayer = req.IdPlayer, FirstName = req.FirstName, LastName = req.LastName, DateOfBirth = req.BirthDate }; Player_Team pt = new Player_Team { IdPlayer = req.IdPlayer, IdTeam = ID, NumonShirt = req.Numonshirt, Comment = req.Comment }; if (!_context.Players.Any(s => s.LastName.Equals(req.LastName))) { _context.Players.Add(player); } _context.Player_Teams.Add(pt); _context.SaveChanges(); return(new OkObjectResult(player)); }
public Student modifyStudent(Student student) { try { context.Attach(student); context.Entry(student).State = EntityState.Modified; context.SaveChanges(); } catch (Exception) { throw; } return(student); }
public void putDoctor(Doctor doc) { context.Doctor.Add(doc); context.SaveChanges(); }