Пример #1
0
 public IActionResult CreateMessage([FromBody] MessageTemplateCreateViewModel messageTemplateVM)
 {
     try
     {
         if (_context.Messages.Any(m => m.Position == messageTemplateVM.Position))
         {
             return(Json(StatusCode(409)));
         }
         var messageTemplate = _map.Map <Message>(messageTemplateVM);
         _context.Add(messageTemplate);
         _context.SaveChanges();
     }
     catch (Exception ex)
     {
         Response.StatusCode = (int)HttpStatusCode.BadRequest;
         return(Json(new { Message = ex.Message }));
     }
     return(Json(Ok()));
     //return Json(messageTemplateVM);
 }
Пример #2
0
        public IActionResult UpdateMessage([FromBody] MessageTemplateCreateViewModel messageTemplateVM, int id)
        {
            try
            {
                if (!_context.Messages.Any(m => m.Position == id))
                {
                    return(Json(BadRequest("Message with PositionID " + id + " is not found")));
                }

                var message = _context.Messages.FirstOrDefault(m => m.Position == id);
                message.Subject  = messageTemplateVM.Subject;
                message.Body     = messageTemplateVM.Body;
                message.Position = messageTemplateVM.Position;
                _context.Messages.Update(message);
                _context.SaveChanges();
                return(Json(StatusCode(204, "Message is updated.")));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }
        }