public async Task <IActionResult> CreateEvent(PostEventBindingModel model) { var result = await _eventService.CreateEvent(GetUserId(), model); if (!result.Succeed) { return(BadRequest(result.Message)); } return(Ok(result.Content)); }
public async Task <ServiceResult <EventViewModel> > CreateEvent(string userId, PostEventBindingModel model) { var agency = await _dbContext .AgencyUsers .Where(x => x.UserId == userId) .Select(x => x.Agency) .FirstOrDefaultAsync(); if (agency == null) { return(new ServiceResult <EventViewModel>("Agency not found")); } var responder = await _dbContext.Responders .FirstOrDefaultAsync(x => x.Code == model.Responder); if (responder == null) { responder = new Responder() { Code = model.Responder, Agency = agency }; _dbContext.Add(responder); } var eventType = await _dbContext.EventTypes .FirstOrDefaultAsync(x => x.Code == model.EventCode); if (eventType == null) { eventType = new EventType() { Code = model.EventCode }; _dbContext.Add(eventType); } var entity = new Event() { DispatchTime = model.DispatchTime, EventTime = model.EventTime, Number = model.EventNumber, Responder = responder, Type = eventType }; _dbContext.Add(entity); var result = await _dbContext.SaveChangesAsync(); if (result > 0) { return(new ServiceResult <EventViewModel>(_mapper.Map <EventViewModel>(entity))); } return(new ServiceResult <EventViewModel>("An error occured.")); }