Exemplo n.º 1
0
        public IActionResult Index()
        {
            var vm = new NewEventVM()
            {
                Event     = new Event(),
                EventList = _context.Events.ToList(),
                Employees = _context.Employees.Include(e => e.Person).ToList(),
                Classes   = _context.Classes.ToList()
            };

            ViewData["EmplyeesList"] = new SelectList(_context.People.Where(p => p.EmployeeID != null), "Id", "FullName");
            return(View(vm));
        }
        public ActionResult Post([FromBody] NewEventVM model)
        {
            Thread.Sleep(4000);

            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(model.Description))
                {
                    return(BadRequest(new OperationResponse()
                    {
                        IsValid = false,
                        Errors = new Dictionary <string, string>()
                        {
                            { "identifier", "Description is empty or invalid." }
                        }
                    }));
                }

                try
                {
                    var newEvent = _eventRepository.Add(model.Description);

                    string newResourceUri = string.Format("/api/users/{0}", newEvent.Id.ToString("N"));

                    return(Created(newResourceUri, newEvent));
                }
                catch
                {
                    return(StatusCode(500, new { message = "An error occurred while attempting to add the event." }));
                }
            }
            else
            {
                var listErrors = ModelState.ToDictionary(
                    m => m.Key,
                    m => m.Value.Errors
                    .Select(s => s.ErrorMessage)
                    .FirstOrDefault(s => s != null)
                    );

                return(BadRequest(new OperationResponse()
                {
                    IsValid = false, Errors = listErrors
                }));
            }
        }