示例#1
0
        public async Task <bool> Add(BeerDto beer, long userId)
        {
            try
            {
                var user = await _beerContext.
                           User.
                           SingleOrDefaultAsync(u => u.Id == userId);

                var beerModel = new BeerEntity {
                    Producer = beer.Producer,
                    Title    = beer.Title,
                    Creater  = user
                };

                var created = await _beerContext.Beer.AddAsync(beerModel);

                var resultCount = await _beerContext.SaveChangesAsync();

                return(resultCount != 0);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(false);
            }
        }
示例#2
0
        public async Task <IActionResult> PostBeer(Beer beer)
        {
            _context.Beers.Add(beer);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetBeer), new { id = beer.Id }, beer));
        }
示例#3
0
        public Task AddBeer(AddBeerToWholesalerCommand command)
        {
            var wholesaler = _beerContext.Wholesalers
                             .Include(w => w.WholesalerBeers)
                             .FirstOrDefault(w => w.Id == command.WholesalerId);

            if (wholesaler == null)
            {
                throw new CustomBadRequestException($"Wholesaler with id {command.WholesalerId} does not exist");
            }

            var beer = _beerContext.Beers.FirstOrDefault(b => b.Id == command.BeerId);

            if (beer == null)
            {
                throw new CustomBadRequestException($"Beer with id {command.BeerId} does not exist");
            }

            if (wholesaler.WholesalerBeers.Any(wb => wb.BeerId == command.BeerId))
            {
                throw new CustomBadRequestException("Wholesaler already sell this beer");
            }

            wholesaler.AddBeer(beer, command.Stock);
            _beerContext.Wholesalers.Update(wholesaler);
            return(_beerContext.SaveChangesAsync());
        }
示例#4
0
        public async Task <HttpResponseMessage> PutBeers(int id, Beers beers)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != beers.Id)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            db.Entry(beers).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                if (!BeersExists(id))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, ex));
                }
                else
                {
                    throw;
                }
            }
            return(Request.CreateResponse(HttpStatusCode.Accepted));
        }
        public async Task <Beer> Create(Beer beer)
        {
            await _context.Beers.AddAsync(beer);

            await _context.SaveChangesAsync();

            return(beer);
        }
示例#6
0
        public async Task <ProductDto> AddProductAsync(ProductDto product)
        {
            var newProduct = await _beerContext.AddAsync(_productTranslator.ToModel(product));

            await _beerContext.SaveChangesAsync();

            var dbProduct = await _beerContext.Products.AsNoTracking().FirstOrDefaultAsync(p => p.EAN == product.EAN);

            return(_productTranslator.ToDto(dbProduct));
        }
        public async Task <IActionResult> Create([Bind("Id,Style")] BeerStyle beerStyle)
        {
            if (ModelState.IsValid)
            {
                _context.Add(beerStyle);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(beerStyle));
        }
示例#8
0
        public async Task <IActionResult> Create([Bind("Name,BreweryId,Abv,StyleId,Notes")] BeerViewModel beer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(beer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            beer.BreweryList = _common.GetBreweryChoices();
            beer.StyleList   = _common.GetStyleChoices();
            return(View(beer));
        }
示例#9
0
        public async Task <IActionResult> PutBrewery([FromRoute] long id, [FromBody] Brewery brewery)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (String.IsNullOrEmpty(brewery.Name) ||
                String.IsNullOrEmpty(brewery.Country) ||
                !brewery.ResourceVersion.HasValue)
            {
                return(BadRequest());
            }

            if (id != brewery.Id)
            {
                brewery.Id = id;
            }

            _context.Entry(brewery).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BreweryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(Conflict());
                }
            }

            brewery.ResourceVersion++;
            _context.Entry(brewery).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(NoContent());
        }
示例#10
0
        public async Task <IActionResult> PutStyleItem([FromRoute] long id, [FromBody] Style styleItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (String.IsNullOrEmpty(styleItem.Name) ||
                !styleItem.OptimalTemperature.HasValue ||
                !styleItem.ResourceVersion.HasValue)
            {
                return(BadRequest());
            }

            if (id != styleItem.Id)
            {
                styleItem.Id = id;
            }
            _context.Entry(styleItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StyleItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(Conflict());
                }
            }

            styleItem.ResourceVersion++;
            _context.Entry(styleItem).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(NoContent());
        }
示例#11
0
        public async Task <IActionResult> Create([Bind("BeerId,Comment,Rating,UserId")] RatingViewModel ratings)
        {
            var _user = await _userManager.GetUserAsync(HttpContext.User);

            if (ModelState.IsValid)
            {
                ratings.UserId = _context.User.Where(x => x.Email == _user.Email).Select(x => x.Id).FirstOrDefault();
                var rating = _common.GetRatingFromViewModel(ratings);
                _context.Add(rating);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(ratings));
        }
示例#12
0
        public async Task <IActionResult> PutUser([FromRoute] long id, [FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (String.IsNullOrEmpty(user.Name) ||
                !user.ResourceVersion.HasValue)
            {
                return(BadRequest());
            }

            var _user = _context.Users.Find(id);

            if (_user == null)
            {
                return(NotFound());
            }

            user.Id        = id;
            user.CartId    = _user.CartId;
            user.Cart      = _user.Cart;
            user.Favorites = _user.Favorites;

            _context.Entry(_user).State = EntityState.Detached;
            _context.Entry(user).State  = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(Conflict());
                }
            }

            user.ResourceVersion++;
            _context.Entry(user).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(NoContent());
        }
示例#13
0
        public async Task <IActionResult> PutBeer([FromBody] Rating rating)
        {
            try
            {
                _context.Ratings.Add(rating);

                if (BeerExists(rating.BeerId) == false)
                {
                    return(NotFound());
                }
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(StatusCode(200, rating));
        }
示例#14
0
        public async Task Delete(int beerId)
        {
            var beer = await GetById(beerId);

            if (beer == null)
            {
                throw new CustomNotFoundException($"Beer with id {beerId} does not exist");
            }

            beer.IsActive = false;
            _context.Beers.Update(beer);

            await _context.SaveChangesAsync();
        }
示例#15
0
        public async Task <IActionResult> Create([Bind("BreweryName,Location")] Brewery brewery)
        {
            if (!_common.BreweryExists(brewery.Id))
            {
                if (ModelState.IsValid)
                {
                    _context.Add(brewery);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(brewery));
        }
示例#16
0
        public async Task <bool> Registration(RegistrationDto registrationDto)
        {
            var userModel = new UserEntity();
            var c         = _beerContext.User.FirstOrDefault(e => e.Email == registrationDto.Email);

            if (c != null)
            {
                return(false);
            }

            userModel.Email    = registrationDto.Email;
            userModel.Password = _cryptoService.EncryptPass(registrationDto.Password);
            userModel.Username = registrationDto.Username;

            _beerContext.User.Add(userModel);

            return(await _beerContext.SaveChangesAsync() != 0);
        }
示例#17
0
        /// <summary>
        /// This method will write changes to the Database for any changes that have happened.
        /// </summary>
        /// <param name="context">Database</param>
        public async void UpdatePreference(BeerContext context)
        {
            try
            {   // Set the Temperature of the Preferred beverage to the StatusViewModel's Temperature,
                // Do a calculation if the temperature is currently set to fahrenheit
                preferredBeverage.Temperature = svm.IsCelsius ? svm.Temperature.Value : ((svm.Temperature.Value - 32) / 1.8);
            }
            catch (Exception)
            {
                throw;
            }

            try
            {   // Write Changes to Database when it is not busy.
                svm.Context.Preference.Update(preferredBeverage);
                await context.SaveChangesAsync();
            }
            catch (SqliteException) { throw; }
        }
示例#18
0
        protected async Task SaveBeer()
        {
            Beer beer;

            if (CurrentBeer.Id == 0)
            {
                beer = new Beer();
                BeerContext.Beers.Add(beer);
            }
            else
            {
                beer = await BeerContext.Beers.FindAsync(int.Parse(Id));
            }

            beer.Name        = CurrentBeer.Name;
            beer.Description = CurrentBeer.Description;

            await BeerContext.SaveChangesAsync();

            NavigationManager.NavigateTo("/");
        }
示例#19
0
        public async Task <IHttpActionResult> PutRecipe(int id, Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != recipe.ID)
            {
                return(BadRequest());
            }

            try
            {
                var originalRecipe = db.Recipes
                                     .Where(p => p.ID == recipe.ID)
                                     .Include(p => p.Recipe_Hops)
                                     .Include(p => p.Recipe_Malts)
                                     .Include(p => p.Recipe_Others)
                                     .SingleOrDefault();

                var parentEntry = db.Entry(originalRecipe);
                parentEntry.CurrentValues.SetValues(recipe);

                //HOPS

                foreach (var hop in recipe.Recipe_Hops)
                {
                    var originalhop = originalRecipe.Recipe_Hops
                                      .Where(c => c.ID == hop.ID && c.ID != 0)
                                      .SingleOrDefault();

                    if (originalhop != null)
                    {
                        var childEntry = db.Entry(originalhop);
                        childEntry.CurrentValues.SetValues(hop);
                    }
                    else
                    {
                        hop.ID = 0;
                        originalRecipe.Recipe_Hops.Add(hop);
                    }
                }

                foreach (var originalhop in originalRecipe.Recipe_Hops.Where(c => c.ID != 0).ToList())
                {
                    if (!recipe.Recipe_Hops.Any(c => c.ID == originalhop.ID))
                    {
                        db.Recipe_Hops.Remove(originalhop);
                    }
                }

                //Malts

                foreach (var malt in recipe.Recipe_Malts)
                {
                    var originalmalt = originalRecipe.Recipe_Malts
                                       .Where(c => c.ID == malt.ID && c.ID != 0)
                                       .SingleOrDefault();

                    if (originalmalt != null)
                    {
                        var childEntry = db.Entry(originalmalt);
                        childEntry.CurrentValues.SetValues(malt);
                    }
                    else
                    {
                        malt.ID = 0;
                        originalRecipe.Recipe_Malts.Add(malt);
                    }
                }

                foreach (var originalmalt in originalRecipe.Recipe_Malts.Where(c => c.ID != 0).ToList())
                {
                    if (!recipe.Recipe_Malts.Any(c => c.ID == originalmalt.ID))
                    {
                        db.Recipe_Malts.Remove(originalmalt);
                    }
                }

                //other

                foreach (var other in recipe.Recipe_Others)
                {
                    var originalother = originalRecipe.Recipe_Others
                                        .Where(c => c.ID == other.ID && c.ID != 0)
                                        .SingleOrDefault();

                    if (originalother != null)
                    {
                        var childEntry = db.Entry(originalother);
                        childEntry.CurrentValues.SetValues(other);
                    }
                    else
                    {
                        other.ID = 0;
                        originalRecipe.Recipe_Others.Add(other);
                    }
                }

                foreach (var originalother in originalRecipe.Recipe_Others.Where(c => c.ID != 0).ToList())
                {
                    if (!recipe.Recipe_Others.Any(c => c.ID == originalother.ID))
                    {
                        db.Recipe_Others.Remove(originalother);
                    }
                }

                await db.SaveChangesAsync();
            }

            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }



            return(StatusCode(HttpStatusCode.NoContent));
        }