Example #1
0
        public async Task <IActionResult> GetPersonalInfo()
        {
            if (!Request.Headers.ContainsKey("id"))
            {
                return(Unauthorized());
            }

            int id = int.Parse(Request.Headers["id"][0]);

            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value))
            {
                return(Unauthorized());
            }

            var user = await _repo.GetUserById(id);

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

            var userForReturn = _mapper.Map <PersonalInfoForReturnDto>(user);

            return(Ok(userForReturn));
        }
Example #2
0
        public async Task <IActionResult> PlaceOrder([FromBody] OrderForCreateDto orderForCreateDto)
        {
            var order = _mapper.Map <Order>(orderForCreateDto);

            if (Request.Headers.ContainsKey("id"))
            {
                var user = await _repo.GetUserById(int.Parse(Request.Headers["id"].First()));

                user.Orders.Add(order);
            }

            foreach (var item in orderForCreateDto.ListOfOrderDetailDto)
            {
                var orderDetail = new OrderDetail();
                var product     = await _repo.GetProductById(item.ProductId);

                orderDetail.PricePerUnit     = item.PricePerUnit;
                orderDetail.Quantity         = item.Quantity;
                orderDetail.Product          = product;
                orderDetail.ProductShortName = item.ProductShortName;
                order.OrderDetails.Add(orderDetail);
            }

            _repo.Add(order);
            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute(nameof(GetOrderById), new { controller = "Order", id = order.Id }, order));
            }

            throw new Exception($"Error, cannot create your order");
        }