public IActionResult CreatePlace() { var model = new CreatePlaceModel(); FillBaseModel(model); FillCreatePlaceModel(model); return(View(model)); }
private void FillCreatePlaceModel(CreatePlaceModel model) { var maybeAllPlaces = _placesService.GetAllPlaces(); if (maybeAllPlaces.Success) { model.ParentPlaces = maybeAllPlaces.Value.ToDictionary(x => x.Id, x => x.PlaceCode); } }
public ActionResult Create() { var model = new CreatePlaceModel { IsServiceDeliveryLocation = true, TypeConcepts = this.placeConceptService.GetPlaceTypeConcepts(healthFacilityMnemonic, placeTypeMnemonic).ToSelectList(this.HttpContext.GetCurrentLanguage()).ToList() }; return(View(model)); }
public IActionResult CreatePlace(CreatePlaceInfo placeInfo) { var newPlace = _mapper.Map <NewPlace>(placeInfo); var createPlaceResult = _placesService.CreatePlace(newPlace); if (createPlaceResult.Success) { return(RedirectToAction("Places", new { id = createPlaceResult.Value.Id })); } var model = new CreatePlaceModel(); FillBaseModel(model); FillCreatePlaceModel(model); model.PlaceInfo = placeInfo; return(View(model)); }
public ActionResult Create(CreatePlaceModel model) { if (model.HasOnlyYearOrPopulation()) { if (string.IsNullOrWhiteSpace(model.Year)) { ModelState.AddModelError("Year", Locale.TargetYearRequired); } if (model.TargetPopulation == null) { ModelState.AddModelError("TargetPopulation", Locale.TargetPopulationRequired); } } if (!string.IsNullOrWhiteSpace(model.Year)) { if (model.ConvertToPopulationYear() == 0) { ModelState.AddModelError("Year", Locale.PopulationYearInvalidFormat); } } if (ModelState.IsValid) { try { var placeToCreate = model.ToPlace(); if (model.SubmitYearAndPopulation()) { var entityExtension = new EntityExtension { ExtensionType = new ExtensionType(Constants.TargetPopulationUrl, typeof(DictionaryExtensionHandler)) { Key = Constants.TargetPopulationExtensionTypeKey }, ExtensionValue = new TargetPopulation(model.ConvertPopulationToULong(), model.ConvertToPopulationYear()) }; placeToCreate.Extensions.Add(entityExtension); } placeToCreate.CreatedByKey = Guid.Parse(this.User.Identity.GetUserId()); var createdPlace = this.entityService.Create(placeToCreate); TempData["success"] = Locale.PlaceSuccessfullyCreated; return(RedirectToAction("ViewPlace", new { id = createdPlace.Key, versionId = createdPlace.VersionKey })); } catch (Exception e) { Trace.TraceError($"Unable to create place: { e }"); } } model.TypeConcepts = this.placeConceptService.GetPlaceTypeConcepts(healthFacilityMnemonic, placeTypeMnemonic).ToSelectList(this.HttpContext.GetCurrentLanguage()).ToList(); this.TempData["error"] = Locale.UnableToCreatePlace; return(View(model)); }
public IActionResult CreatePlace([FromBody] CreatePlaceModel place) { if (place == null || place.Title == null || place.Country == null || place.City == null || place.Street == null || place.Number == null) { return(BadRequest("Invalid request")); } string get_email = ""; var identity = HttpContext.User.Identity as ClaimsIdentity; IEnumerable <Claim> claim = identity.Claims; var usernameClaim = claim .Where(x => x.Type == ClaimTypes.Name) .FirstOrDefault(); if (usernameClaim != null) { get_email = usernameClaim.Value; } if (get_email.Length < 1) { return(Unauthorized("Invalid user")); } Users user = LoadUser(get_email); if (user == null) { return(Unauthorized("Invalid user")); } if (place.Title.Length < 1) { return(Unauthorized("Missing title")); } if (place.Country.Length < 1) { return(Unauthorized("Missing country name")); } if (place.City.Length < 1) { return(Unauthorized("Missing city name")); } if (place.Street.Length < 1) { return(Unauthorized("Missing street name")); } if (place.Number.Length < 1) { return(Unauthorized("Missing number name")); } if (String.IsNullOrEmpty(place.Description)) { place.Description = null; } TimeSpan timeOpen; TimeSpan timeClose; if (place.TimeOC1 != null) { timeOpen = TimeSpan.Parse(place.TimeOC1[0]); timeClose = TimeSpan.Parse(place.TimeOC1[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } if (place.TimeOC2 != null) { timeOpen = TimeSpan.Parse(place.TimeOC2[0]); timeClose = TimeSpan.Parse(place.TimeOC2[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } if (place.TimeOC3 != null) { timeOpen = TimeSpan.Parse(place.TimeOC3[0]); timeClose = TimeSpan.Parse(place.TimeOC3[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } if (place.TimeOC4 != null) { timeOpen = TimeSpan.Parse(place.TimeOC4[0]); timeClose = TimeSpan.Parse(place.TimeOC4[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } if (place.TimeOC5 != null) { timeOpen = TimeSpan.Parse(place.TimeOC5[0]); timeClose = TimeSpan.Parse(place.TimeOC5[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } if (place.TimeOC6 != null) { timeOpen = TimeSpan.Parse(place.TimeOC6[0]); timeClose = TimeSpan.Parse(place.TimeOC6[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } if (place.TimeOC7 != null) { timeOpen = TimeSpan.Parse(place.TimeOC7[0]); timeClose = TimeSpan.Parse(place.TimeOC7[1]); if (timeOpen >= timeClose) { return(Unauthorized("Wrong time")); } } using (var _context = new AppDBContext()) { using (var _contextTransaction = _context.Database.BeginTransaction()) { try { Place new_place = new Place(); new_place.id_user = user.id_user; new_place.title = place.Title; new_place.description = place.Description; new_place.multi_time = true; new_place.Place_data_multitime = new List <Place_data_multitime>(); if (place.TimeOC1 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.MONDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC1[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC1[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } if (place.TimeOC2 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.TUESDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC2[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC2[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } if (place.TimeOC3 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.WEDNESDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC3[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC3[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } if (place.TimeOC4 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.THURSDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC4[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC4[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } if (place.TimeOC5 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.FRIDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC5[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC5[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } if (place.TimeOC6 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.SATURDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC6[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC6[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } if (place.TimeOC7 != null) { Place_data_multitime new_multitime = new Place_data_multitime(); new_multitime.day_week = E_day_week.SUNDAY; new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC7[0]}:00"); new_multitime.end_date = TimeSpan.Parse($"{place.TimeOC7[1]}:00"); new_place.Place_data_multitime.Add(new_multitime); } new_place.Place_address = new Place_address(); new_place.Place_address.country = place.Country; new_place.Place_address.city = place.City; new_place.Place_address.street = place.Street; new_place.Place_address.number = place.Number; _context.Place.Add(new_place); _context.SaveChanges(); _contextTransaction.Commit(); } catch { _contextTransaction.Rollback(); return(BadRequest("Can't create new place")); } } } return(Ok()); }