public async Task <Guid> Add(WorkshopAccountViewModel workshopViewModel, ApplicationUser user) { var workshopData = Mapper.Map <WorkshopData>(workshopViewModel); workshopData.UserID = user.Id; var workshopId = Guid.Empty; AnchorType anchor; using (var dbContext = _dbContextScope.Create()) { //check if the same instance is already exist: verify by NAME var gmapAddress = await geoLocator.DecodeAddress(workshopViewModel.Address.GooglePlaceId); var city = workshopData.Address?.City?.Ru; var country = ""; if (gmapAddress != null) { workshopData.Address = new AddressData { Building = gmapAddress.Number, GooglePlaceId = workshopViewModel.Address.GooglePlaceId, Street = gmapAddress.Street, }; city = gmapAddress.City; country = gmapAddress.Country; } workshopData.Address.CityID = VerifyOrAddCity(city, country); if (workshopData.IsPublished) { workshopData.IsPublished = false; } anchor = new AnchorType { Quantity = workshopData.AnchorNumber, Price = workshopData.PayHour, Name = "Hoist", Description = "For cars", }; workshopData.Anchors.Add(anchor); workshopData.Slug = HandleGenerator.Generate(12); workshopData.AccessCode = HandleGenerator.Generate(6); workshopId = _workshopAccountRepository.Add(workshopData); dbContext.SaveChanges(); } PublishWorkshopEvent <WorkshopCreated>(workshopData); if (anchor != null) { PublishAnchorCreated(workshopId, anchor); } await accountService.AddClaim(user, ApplicationClaims.Accomplished); return(workshopId); }
public void Update(WorkshopAccountViewModel workshopViewModel) { var wsUpdated = Mapper.Map <WorkshopData>(workshopViewModel); AnchorType hoists = null; using (var dbContext = _dbContextScope.Create()) { var wsCurrent = filterRepo.FindById(wsUpdated.ID); if (wsCurrent == null) { throw new ArgumentException(string.Format("Provided ID {0} doesn't exist in DB", wsUpdated.ID)); } _workshopAccountRepository.LoadAnchors(wsCurrent); //TODO: temp decision with Address until Google PathID will be integrated _workshopAccountRepository.LoadAddress(wsCurrent); if (wsCurrent.Address != null && wsCurrent.Address.City.Ru != wsUpdated.Address.City.Ru) { //verify if new city exists in DB var cityId = _addressService.GetCityByName(wsUpdated.Address.City.Ru); if (cityId == null) { throw new ArgumentException(string.Format("the city {0} hasn't been added to database", wsUpdated.Address.City.Ru)); } wsUpdated.Address.CityID = cityId; } var categories = wsUpdated.WorkshopCategories; if (categories != null) { foreach (var c in categories) { c.WorkshopID = wsUpdated.ID; } } var autobrands = wsUpdated.WorkshopAutobrands; if (autobrands != null) { foreach (var w in autobrands) { w.WorkshopID = wsUpdated.ID; } } if (wsCurrent.AnchorNumber != wsUpdated.AnchorNumber || wsCurrent.PayHour != wsUpdated.PayHour) { //TODO: means that we have only Hoist anchors hoists = wsCurrent.Anchors.FirstOrDefault(); if (hoists != null) { hoists.Quantity = wsUpdated.AnchorNumber; hoists.Price = wsUpdated.PayHour; } } if (wsUpdated.RegisterDate == null || wsUpdated.RegisterDate == DateTime.MinValue) { wsUpdated.RegisterDate = wsCurrent.RegisterDate; } wsUpdated.WasEverPublished = wsCurrent.WasEverPublished; _workshopAccountRepository.Update(wsUpdated, wsCurrent); dbContext.SaveChanges(); } PublishWorkshopEvent <WorkshopUpdated>(wsUpdated); if (wsUpdated.WasEverPublished && hoists != null) { PublishAnchorUpdated(wsUpdated.ID, hoists); } }