Пример #1
0
        public bool CreateWine(CreateWineRequest request, out IEnumerable <string> errors)
        {
            errors = _validateWineRepository.ValidateWineModel(request);
            if (errors.Any())
            {
                return(false);
            }

            return(_createUpdateWineRepository.CreateWine(request));
        }
Пример #2
0
        public bool CreateWine(CreateWineRequest request)
        {
            try
            {
                var wineList = MapRequestToWineListModel(request);
                _dataContext.Winelists.Add(wineList);

                var persisted = _dataContext.SaveChanges();
                return(persisted >= 1);
            }
            catch (DbUpdateConcurrencyException)
            {
                return(false);
            }
            catch (DbUpdateException)
            {
                return(false);
            }
        }
        public IEnumerable <string> ValidateWineModel(CreateWineRequest wine)
        {
            var errors = new List <string>();

            if (wine.VineyardId != null)
            {
                var vineyard = _dataContext.Vineyards.Find(wine.VineyardId);
                if (vineyard == null)
                {
                    // Existing vineyard cannot be found
                    errors.Add("Vineyard id: " + wine.VineyardId + " could not be found");
                }
            }

            if (wine.WineTypeId != null)
            {
                var wineType = _dataContext.Vineyards.Find(wine.WineTypeId);
                if (wineType == null)
                {
                    // Existing wineType cannot be found
                    errors.Add("Wine type id: " + wine.WineTypeId + " could not be found");
                }
            }

            if (wine.RegionId != null)
            {
                var region = _dataContext.Vineyards.Find(wine.RegionId);
                if (region == null)
                {
                    // Existing region cannot be found
                    errors.Add("Region id: " + wine.RegionId + " could not be found");
                }
            }

            if (!ValidateYear(wine.Vintage))
            {
                errors.Add("Vintage is not a valid year");
            }

            if (!ValidateYear(wine.YearBought))
            {
                errors.Add("Year bought is not a valid year");
            }

            if (!ValidateYear(wine.DrinkFrom))
            {
                errors.Add("Drink from is not a valid year");
            }

            if (!ValidateYear(wine.DrinkTo))
            {
                errors.Add("Drink to is not a valid year");
            }

            if (!ValidateRating(wine.Rating))
            {
                errors.Add("Rating is not between 1 and 100");
            }

            if (!ValidateBottleSize(wine.BottleSize))
            {
                errors.Add("Bottle size is invalid");
            }

            return(errors);
        }
Пример #4
0
        private Models.Winelist MapRequestToWineListModel(
            CreateWineRequest request,
            Models.Winelist existingWine = null)
        {
            var newWine = existingWine ?? new Models.Winelist();

            newWine.Vintage        = (short?)request.Vintage;
            newWine.Winename       = request.WineName;
            newWine.Percentalcohol = request.PercentAlcohol;
            newWine.Pricepaid      = request.PricePaid;
            newWine.Yearbought     = (short?)request.YearBought;
            newWine.Bottlesize     = (short?)request.BottleSize;
            newWine.Drinkrangefrom = (short?)request.DrinkFrom;
            newWine.Drinkrangeto   = (short?)request.DrinkTo;
            newWine.Notes          = request.Notes;
            newWine.Rating         = (short?)request.Rating;
            newWine.Locations      = MapLocations(request.Locations)?.ToList();

            // Adds Vineyard to new wine
            if (request.VineyardId != null)
            {
                newWine.Vineyardid = request.VineyardId;
            }
            else if (!string.IsNullOrEmpty(request.Vineyard))
            {
                var existingVineyard = _dataContext.Vineyards.FirstOrDefault(x => x.Vineyard1 == request.Vineyard);
                newWine.Vineyard = existingVineyard ?? new Models.Vineyard
                {
                    Vineyard1 = request.Vineyard
                };
            }

            // Adds Region to new wine
            if (request.RegionId != null)
            {
                newWine.Regionid = request.RegionId;
            }
            else if (!string.IsNullOrEmpty(request.Region))
            {
                var existingRegion = _dataContext.Regions.FirstOrDefault(x => x.Region1 == request.Region);
                newWine.Region = existingRegion ?? new Models.Region
                {
                    Region1 = request.Region
                };
            }

            // Adds Wine type to new wine
            if (request.WineTypeId != null)
            {
                newWine.Winetypeid = request.WineTypeId;
            }
            else if (!string.IsNullOrEmpty(request.WineType))
            {
                var existingWineType = _dataContext.Winetypes.FirstOrDefault(x => x.Winetype1 == request.WineType);
                newWine.Winetype = existingWineType ?? new Models.Winetype
                {
                    Winetype1 = request.WineType
                };
            }

            return(newWine);
        }