public async Task <OperationResult> AddSiteToSettingsAsync(int siteId) { var theCore = await _coreService.GetCore(); await using var sdkDbContext = theCore.DbContextHelper.GetDbContext(); try { if (await sdkDbContext.Sites.AnyAsync(x => x.MicrotingUid == siteId)) { var assignedSite = new AssignedSite { SiteUid = siteId, UpdatedByUserId = _userService.UserId, CreatedByUserId = _userService.UserId, }; await assignedSite.Create(_dbContext); return(new OperationResult(true, _inventoryLocalizationService.GetString("SettingsHaveBeenUpdatedSuccessfully"))); } throw new ArgumentException($"Site with Uid = {siteId} doesn't exist"); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, _inventoryLocalizationService.GetString("ErrorWhileUpdatingSettings"))); } }
public async Task <OperationResult> AddSiteToSettingsAsync(int siteId) { var option = _options.Value; var newTaskId = option.NewTaskId; var folderId = option.FolderId; var theCore = await _core.GetCore(); await using var sdkDbContext = theCore.DbContextHelper.GetDbContext(); var folder = await sdkDbContext.Folders.SingleOrDefaultAsync(x => x.Id == folderId); if (folder == null) { return(new OperationResult(false, _workOrdersLocalizationService.GetString("FolderNotExist"))); } var site = await sdkDbContext.Sites.SingleOrDefaultAsync(x => x.MicrotingUid == siteId); if (site == null) { return(new OperationResult(false, _workOrdersLocalizationService.GetString("SiteNotFind"))); } var language = await sdkDbContext.Languages.SingleAsync(x => x.Id == site.LanguageId); var mainElement = await theCore.ReadeForm(newTaskId, language); switch (language.Name) { case LanguageNames.Danish: { mainElement.Label = "Ny opgave"; break; } case LanguageNames.English: { mainElement.Label = "New task"; break; } case LanguageNames.German: { mainElement.Label = "Neue Aufgabe"; break; } } mainElement.CheckListFolderName = folder.MicrotingUid.ToString(); mainElement.EndDate = DateTime.UtcNow.AddYears(10); mainElement.Repeated = 0; mainElement.PushMessageTitle = mainElement.Label; //await using IDbContextTransaction transaction = await _dbContext.Database.BeginTransactionAsync(); try { var caseId = await theCore.CaseCreate(mainElement, "", siteId, folderId); var assignedSite = new AssignedSite() { SiteMicrotingUid = siteId, CaseMicrotingUid = (int)caseId }; await assignedSite.Create(_dbContext); //await transaction.CommitAsync(); await _bus.SendLocal(new SiteAdded(siteId)); return(new OperationResult(true, _workOrdersLocalizationService.GetString("SiteAddedSuccessfully"))); } catch (Exception e) { //await transaction.RollbackAsync(); Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, _workOrdersLocalizationService.GetString("ErrorWhileAddingSiteToSettings"))); } }