public static Model.Dogs MapDogs(Core.Model.Dogs dogs) { return(new Model.Dogs { Id = dogs.Id, DogTypeId = dogs.DogTypeId, UserId = dogs.UserId, PetName = dogs.PetName, Hunger = dogs.Hunger, Mood = dogs.Mood, IsAlive = dogs.IsAlive, AdoptionDate = dogs.AdoptionDate, Age = dogs.Age, Energy = dogs.Energy, TricksProgress = dogs.TricksProgress.Select(MapTricksProgress).ToList() }); }
/// <summary> /// Adding a dog to the DB /// </summary> /// <param name="dogs"></param> /// <returns>The Dog added</returns> public async Task <Core.Model.Dogs> PostDogsAsync(Core.Model.Dogs dogs) { var newDog = new Dogs { UserId = dogs.UserId, DogTypeId = dogs.DogTypeId, PetName = dogs.PetName }; _logger.LogInformation("Addng an Dog to the DB"); _dbContext.Add(newDog); await _dbContext.SaveChangesAsync(); // After the Entity is added, return the Entity // by getting the max id (newest Entity). int id = await _dbContext.Dogs.MaxAsync(d => d.Id); var nd = await _dbContext.Dogs.FindAsync(id); return(Mapper.MapDogs(nd)); }
/// <summary> /// Updating a Dog in the DB /// based on Dogs ID /// </summary> /// <param name="id"></param> /// <param name="dogs"></param> /// <returns>The Updated Dog</returns> public async Task <Core.Model.Dogs> PutDogsAsync(int id, Core.Model.Dogs dogs) { var oldDog = await _dbContext.Dogs.FindAsync(id); if (oldDog is null) { _logger.LogError("The Dog with ID {dogsId} does not exist", dogs.Id); return(null); } else { _logger.LogInformation("Updating the Dog with ID {dogsId}.", dogs.Id); // This method only marks the properties changed // as modified. var newDog = Mapper.MapDogs(dogs); _dbContext.Entry(oldDog).CurrentValues.SetValues(newDog); await _dbContext.SaveChangesAsync(); return(Mapper.MapDogs(newDog)); } }