public ActionResult AddEvent(EventViewData model) { JsonViewData viewData = Validate <JsonViewData>( new Validation(() => string.IsNullOrWhiteSpace(model.EventLink.NullSafe()), "Link do wydarzenia nie może być pusty."), new Validation(() => string.IsNullOrWhiteSpace(model.EventName.NullSafe()), "Tytuł wydarzenia nie może być pusty."), new Validation(() => model.EventUserEmail.NullSafe().IsEmail() == false, "Nieprawidłowy adres e-mail."), new Validation(() => model.Id.ToGuid() != Guid.Empty, "Id wydarzenia nie może być podane"), new Validation(() => !model.EventEndDate.IsLaterThan(model.EventDate), "Nieprawidłowa data zakończenia wydarzenia.") ); if (viewData == null) { try { var eventApproveStatus = CurrentUser != null && CurrentUser.IsAdministrator() && model.IsApproved; if (eventApproveStatus) { model.GoogleEventId = _googleService.EventApproved(new CommingEvent(model.EventName, model.EventLink, model.GoogleEventId, model.EventDate, model.EventEndDate, model.EventCity, model.EventPlace, model.EventLead, model.IsOnline)); } using (IUnitOfWork unitOfWork = UnitOfWork.Begin()) { var commingEvent = _factory.CreateCommingEvent( model.EventUserEmail, model.EventLink, model.EventName, model.GoogleEventId, model.EventDate, model.EventEndDate, model.EventCity, model.EventPlace, model.EventLead, eventApproveStatus, model.IsOnline ); _commingEventRepository.Add(commingEvent); unitOfWork.Commit(); _aggregator.GetEvent <UpcommingEventEvent>() .Publish(new UpcommingEventEventArgs(model.EventName, model.EventLink)); Log.Info("Event registered: {0}", commingEvent.EventName); viewData = new JsonViewData { isSuccessful = true }; } } catch (Exception e) { Log.Exception(e); viewData = new JsonViewData { errorMessage = FormatStrings.UnknownError.FormatWith("dodawania wydarzenia") }; } } return(Json(viewData)); }
public IActionResult Save(IFormCollection form, EventViewData model) { bool isSuccessful = false; string errorMessage = ""; Event postData = null; try { postData = CreateEvent(form); using (var client = new HttpClient()) { client.BaseAddress = new Uri(_serviceSettings.Value.MainUrl); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _serviceSettings.Value.FleetprimeAwareToken); var jsonInString = JsonConvert.SerializeObject(postData); var responseTask = client.PostAsync("wm-iot", new StringContent(jsonInString, Encoding.UTF8, "application/json")); responseTask.Wait(); var result = responseTask.Result; isSuccessful = result.IsSuccessStatusCode; if (result.IsSuccessStatusCode) { ViewData.Add("Success", string.Format("The data with transactionId: \"{0}\" has been pushed to the server.", postData.TransactionId)); } else { var readTask = result.Content.ReadAsAsync <ErrorModel>(); readTask.Wait(); var modelResult = readTask.Result; string errorMessages = ""; if (modelResult.Messages != null) { errorMessages = String.Join(",", modelResult.Messages.Select(i => i.ToString()).ToArray()); } errorMessage = string.Format("The api server ( {0} ) returned an error: {1} , {2}", _serviceSettings.Value.MainUrl.ToString(), modelResult.Status, errorMessages); ViewData.Add("Error", errorMessage); } } return(View("Index", _testData.CreateEventViewData())); } catch (Exception e) { errorMessage = string.Format("Cannot connect to the api server ( {0} ). {1}", _serviceSettings.Value.MainUrl.ToString(), e.Message); ViewData.Add("Error", errorMessage); return(View("Index", _testData.CreateEventViewData())); } finally { if (postData != null) { var eventLog = TransformEventToEventLog(isSuccessful, errorMessage, postData); _wasteManagementDbContext.EventLogs.Add(eventLog); _wasteManagementDbContext.SaveChanges(); } } }
private Event CreateEvent(IFormCollection form) { EventViewData viewData = _testData.CreateEventViewData(); var addresses = viewData.Addresses; var rejectReasons = viewData.RejectReasons; string transactionId = Guid.NewGuid().ToString(); int addressIndex = int.Parse(form["Address"]); var address = addresses[addressIndex]; return(new Event() { Vin = form["Vin"], AccountId = form["AccountId"], EventType = form["EventType"], TransactionId = transactionId, DateTime = DateTime.Now.ToUniversalTime(), Latitude = address.Latitude, Longitude = address.Longitude, ContainerColor = form["ContainerColor"], ContainerSize = form["ContainerSize"], ContainerType = form["ContainerType"], Address = new Address { Street1 = address.Street1, Street2 = address.Street2, City = address.City, State = address.State, ZipCode = address.ZipCode, Country = address.Country }, ExceptionDetails = CreateExceptionDetails(form, rejectReasons) }); }
public ActionResult EditEvent(EventViewData model) { if (model.EventLead == null) { model.EventLead = "Brak opisu"; } JsonViewData viewData = Validate <JsonViewData>( new Validation(() => string.IsNullOrEmpty(model.EventLink.NullSafe()), "Link wydarzenia nie może być pusty."), new Validation(() => string.IsNullOrEmpty(model.EventName.NullSafe()), "Tytuł wydarzenia nie może być pusty."), new Validation(() => !model.EventUserEmail.NullSafe().IsEmail(), "Niepoprawny adres e-mail."), new Validation(() => CurrentUser.IsAdministrator() == false, "Nie możesz edytować tego wydarzenia."), new Validation(() => model.Id.NullSafe().ToGuid().IsEmpty(), "Nieprawidłowy identyfikator wydarzenia."), new Validation(() => !model.EventEndDate.IsLaterThan(model.EventDate), "Nieprawidłowa data zakończenia wydarzenia.") ); if (viewData == null) { try { using (IUnitOfWork unitOfWork = UnitOfWork.Begin()) { ICommingEvent commingEvent = _commingEventRepository.FindById(model.Id.ToGuid()); var eventApproveStatus = CurrentUser.IsAdministrator() && model.IsApproved; if (commingEvent == null) { viewData = new JsonViewData { errorMessage = "Podane wydarzenie nie istnieje." }; } else { if (model.IsApproved) { if (string.IsNullOrEmpty(commingEvent.GoogleEventId)) { var upcomingEvent = new CommingEvent(commingEvent.EventName, commingEvent.EventLink, commingEvent.GoogleEventId, commingEvent.EventDate, commingEvent.EventEndDate.Value, commingEvent.EventCity, commingEvent.EventPlace, commingEvent.EventLead, model.IsOnline); model.GoogleEventId = _googleService.EventApproved(upcomingEvent); } else { var upcomingEvent = new CommingEvent(model.EventName, model.EventLink, commingEvent.GoogleEventId, model.EventDate, model.EventEndDate, model.EventCity, model.EventPlace, model.EventLead, model.IsOnline); model.GoogleEventId = _googleService.EditEvent(upcomingEvent); } } else if (!string.IsNullOrEmpty(commingEvent.GoogleEventId)) { _googleService.DeleteEvent(commingEvent.GoogleEventId); model.GoogleEventId = null; } _commingEventRepository.EditEvent( commingEvent, model.EventUserEmail.NullSafe(), model.EventLink.NullSafe(), model.GoogleEventId, model.EventName.NullSafe(), model.EventDate, model.EventEndDate, model.EventCity, model.EventPlace, model.EventLead, eventApproveStatus, model.IsOnline ); unitOfWork.Commit(); viewData = new JsonViewData { isSuccessful = true }; } } } catch (Exception e) { Log.Exception(e); viewData = new JsonViewData { errorMessage = FormatStrings.UnknownError.FormatWith("edycji wydarzenia.") }; } } return(Json(viewData)); }