public bool UpdateWine(UpdateWineRequest request) { // Check wine already exists var existingWine = _dataContext.Winelists.Find(request.Id); if (existingWine == null) { return(false); } // Clear any existing locations _dataContext.Entry(existingWine).Collection(w => w.Locations).Load(); if (existingWine.Locations?.Any() ?? false) { _dataContext.Locations.RemoveRange(existingWine.Locations); } try { // Populate data in WineList model, and persist var wineList = MapRequestToWineListModel(request, existingWine); _dataContext.Winelists.Update(wineList); var persisted = _dataContext.SaveChanges(); return(persisted >= 1); } catch (DbUpdateConcurrencyException) { return(false); } catch (DbUpdateException) { return(false); } }
public IEnumerable <string> ValidateWineModel(UpdateWineRequest request) { var wine = _dataContext.Winelists.Find(request.Id); return(wine != null ? ValidateWineModel(request as CreateWineRequest) : new[] { "Wine id: " + request.Id + " could not be found." }); }
public bool UpdateWine(UpdateWineRequest request, out IEnumerable <string> errors) { errors = _validateWineRepository.ValidateWineModel(request); if (errors.Any()) { return(false); } return(_createUpdateWineRepository.UpdateWine(request)); }