示例#1
0
        public IActionResult Post([FromBody] OrderViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newOrder = _mapper.Map <OrderViewModel, Order>(model);

                    if (newOrder.OrderDate == DateTime.MinValue)
                    {
                        newOrder.OrderDate = DateTime.Now;
                    }

                    _repository.AddEntity(newOrder);
                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/orders/{newOrder.Id}", _mapper.Map <Order, OrderViewModel>(newOrder)));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a new order: {ex}");
            }

            return(BadRequest("Failed to save a new order"));
        }
示例#2
0
        public IActionResult CreateMessage(CreateMessageViewModel model)
        {
            if (ModelState.IsValid)
            {
                Message message = new Message()
                {
                    IsRead      = model.IsRead,
                    Receiver    = model.Receiver,
                    Sender      = User.Identity.Name,
                    TimeSent    = model.TimeSent,
                    MessageItem = model.MessageItem,
                    Subject     = model.Subject,
                };

                _repository.AddEntity(message);
                _repository.SaveAll();

                ModelState.Clear();
                ViewBag.Message = $"Message sent to {model.Receiver}";
            }

            return(View());
        }