public async Task <IActionResult> Post([FromBody] OrderViewModel model)
        {
            try
            {
                _logger.LogError($"Model:{model}");
                if (ModelState.IsValid)
                {
                    // "OrderViewModel" is used to validate and recieve the Order Data
                    //posted from the Front End,before it will be stored in DB as actual "Order"

                    //Mapping  "OrderViewModel" to "Order"
                    var newOrder = _mapper.Map <OrderViewModel, Order>(model);


                    //if they didn't specify the date
                    if (newOrder.OrderDate == DateTime.MinValue)
                    {
                        newOrder.OrderDate = DateTime.Now;
                    }

                    //Add UserId to the Order
                    ClaimsPrincipal currentUser      = this.User; //get current logged in user
                    var             currentUserEmail = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
                    StoreUser       user             = await _userManager.FindByEmailAsync(currentUserEmail);

                    newOrder.User = user;


                    //ADDING TO REPOSITORY

                    // _repository.AddEntity(newOrder);
                    _repository.AddOrder(newOrder);

                    //mapping  back from OrderModel to OrderViewModel,using IMapper
                    //Returning our View Model with 201 code


                    var vm = _mapper.Map <Order, OrderViewModel>(newOrder);

                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/orders/{vm.OrderID}", vm)); //"Created" matching 201 code
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a new order:{ex}");
            }

            return(BadRequest("Failed to save new order"));
        }
示例#2
0
        public async Task <IActionResult> Post([FromBody] Review model)
        //public async Task<IActionResult> Post([FromForm] FormCollection model)
        {
            try
            {
                //Review model = new Review();
                //model.ReviewerName = request["reviewer_name"];
                //model.Content = request["content"];
                //model.Email= request["email"];
                //model.Rating = Int32.Parse(request["rating"]);

                _logger.LogError($"Model:{model}");
                if (ModelState.IsValid)
                {
                    //if on Front End ,they didn't specify the date
                    if (model.Date == DateTime.MinValue)
                    {
                        model.Date = DateTime.Now;
                    }


                    //ADDING TO REPOSITORY

                    _repository.AddReview(model);


                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/reviews/{model.Id}", model)); //"Created" matching 201 code
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a new review:{ex}");
            }

            return(BadRequest("Failed to save new review"));
        }