Example #1
0
        private async Task Buy()
        {
            Console.WriteLine("Enter name");
            var firstName = Console.ReadLine();

            Console.WriteLine("Enter surname");
            var surname = Console.ReadLine();

            Console.WriteLine("Enter e-mail");
            var email = Console.ReadLine();

            Console.WriteLine("Enter phone (Format: xxxxxxxxxxxx)");
            var phone = Console.ReadLine();

            Console.WriteLine("Enter address");
            var address = Console.ReadLine();

            Console.WriteLine("Enter zipcode");
            var zipCode = Console.ReadLine();


            var buyerDto = new Buyer
            {
                Address     = address,
                FirstName   = firstName,
                SecondName  = surname,
                Email       = email,
                PhoneNumber = phone,
                ZipCode     = zipCode
            };

            await _buyerManager.Add(buyerDto);

            Console.WriteLine("Enter amount");
            var amount = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());

            var orderDto = new Order
            {
                ProductId = ProductId,
                BuyerId   = buyerDto.Id,
                Amount    = amount
            };

            try
            {
                var productDto = await _productManager.GetById(ProductId);

                productDto.AmountInStorage -= amount;

                await _productManager.Update(productDto);

                await _orderManager.Add(orderDto);
            }
            catch (ValidationException)
            {
                await _buyerManager.Delete(buyerDto.Id);

                throw;
            }
        }
        public async Task <ActionResult <Buyer> > Post([FromBody] Buyer buyer)
        {
            try
            {
                await _buyerManager.Add(buyer);

                var user = await _userManager.FindByEmailAsync(User.Claims.FirstOrDefault(claim => claim.Type == JwtRegisteredClaimNames.UniqueName).Value);

                user.BuyerId = int.Parse(User.Claims.FirstOrDefault(claim => claim.Type == BuyerClaim.BuyerId).Value);

                await _userManager.UpdateAsync(user);

                return(Ok(buyer));
            }
            catch (Exception exception)
            {
                _logger.LogError($"Error occured during creating buyer. Exception: {exception.Message}");
                return(BadRequest());
            }
        }
        public async Task <IActionResult> Create(BuyerViewModel buyerViewModel)
        {
            try
            {
                var buyer = _mapper.Map <BuyerViewModel, Buyer>(buyerViewModel);

                await _buyerManager.Add(buyer);

                var user = await _userManager.GetUserAsync(User);

                user.BuyerId = buyer.Id;

                await _userManager.UpdateAsync(user);

                return(RedirectToAction("Index", "Products"));
            }
            catch (Exception exception)
            {
                _logger.LogError($"Error occured during creating buyer. Exception: {exception.Message}");
                return(View(buyerViewModel));
            }
        }