/// <summary> /// Add destination point to the Azure DB. /// file name is the absolute path of the file. /// </summary> /// <param name="fileName"></param> /// <param name="destinationPoint"></param> /// <returns></returns> public async Task <DestinationPoint> AddDestinationPointAsync(string fileName, string documentId, DestinationPoint destinationPoint) { try { ///Get destination catalog by file server absolute path. var destinationCatalog = _dbContext.DestinationCatalogs.FirstOrDefault(o => o.DocumentId == documentId); bool addDestinationCatalog = (destinationCatalog == null); if (addDestinationCatalog) { try { destinationCatalog = new DestinationCatalog() { Name = fileName, DocumentId = documentId }; _dbContext.DestinationCatalogs.Add(destinationCatalog); } catch (Exception ex) { var entity = new LogEntity() { LogId = "40002", Action = Constant.ACTIONTYPE_ADD, ActionType = ActionTypeEnum.ErrorLog, PointType = Constant.POINTYTPE_DESTINATIONCATALOG, Message = ".Net Error", }; entity.Subject = $"{entity.LogId} - {entity.Action} - {entity.PointType} - Error"; await _logService.WriteLogAsync(entity); throw new ApplicationException("Add Source Catalog failed", ex); } } destinationPoint.Created = DateTime.Now.ToUniversalTime().ToPSTDateTime(); destinationPoint.Creator = _userProfileService.GetCurrentUser().Username; destinationCatalog.DestinationPoints.Add(destinationPoint); _dbContext.SourcePoints.Attach(destinationPoint.ReferencedSourcePoint); _dbContext.DestinationPoints.Add(destinationPoint); foreach (var formatId in destinationPoint.CustomFormats) { _dbContext.CustomFormats.Attach(formatId); } await _dbContext.SaveChangesAsync(); await _dbContext.Entry(destinationPoint.ReferencedSourcePoint).ReloadAsync(); foreach (var customFormatItem in destinationPoint.CustomFormats) { await _dbContext.Entry(customFormatItem).ReloadAsync(); } if (addDestinationCatalog) { await _logService.WriteLogAsync(new LogEntity() { LogId = "40001", Action = Constant.ACTIONTYPE_ADD, PointType = Constant.POINTYTPE_DESTINATIONCATALOG, ActionType = ActionTypeEnum.AuditLog, Message = $"Add destination catalog {destinationCatalog.Name}." }); } await _logService.WriteLogAsync(new LogEntity() { LogId = "20001", Action = Constant.ACTIONTYPE_ADD, PointType = Constant.POINTTYPE_DESTINATIONPOINT, ActionType = ActionTypeEnum.AuditLog, Message = $"Create destination point value: {destinationPoint.ReferencedSourcePoint.Value} in the word file named:{destinationCatalog.FileName} by {_userProfileService.GetCurrentUser().Username}" }); destinationPoint = await _dbContext.DestinationPoints .Include(o => o.ReferencedSourcePoint) .Include(o => o.ReferencedSourcePoint.Catalog) .Include(o => o.ReferencedSourcePoint.PublishedHistories) .Include(o => o.CustomFormats).FirstAsync(o => o.Id == destinationPoint.Id); destinationPoint.ReferencedSourcePoint.PublishedHistories = destinationPoint.ReferencedSourcePoint.PublishedHistories.OrderByDescending(p => p.PublishedDate).ToArray(); destinationPoint.ReferencedSourcePoint.SerializeCatalog = true; destinationPoint.ReferencedSourcePoint.Catalog.SerializeSourcePoints = false; destinationPoint.CustomFormats = destinationPoint.CustomFormats.OrderBy(c => c.GroupOrderBy).ToArray(); await _logService.WriteLogAsync(new LogEntity() { LogId = "30002", Action = Constant.ACTIONTYPE_GET, PointType = Constant.POINTTYPE_SOURCECATALOGLIST, ActionType = ActionTypeEnum.AuditLog, Message = $"Get source catalogs." }); return(destinationPoint); } catch (ApplicationException ex) { throw ex.InnerException; } catch (Exception ex) { var logEntity = new LogEntity() { LogId = "20003", Action = Constant.ACTIONTYPE_ADD, ActionType = ActionTypeEnum.ErrorLog, PointType = Constant.POINTTYPE_DESTINATIONPOINT, Message = ".Net Error", Detail = ex.ToString() }; logEntity.Subject = $"{logEntity.LogId} - {logEntity.Action} - {logEntity.PointType} - Error"; await _logService.WriteLogAsync(logEntity); throw ex; } }
/// <summary> /// Update the source point. /// </summary> /// <param name="groupIds"></param> /// <param name="sourcePoint"></param> /// <returns></returns> public async Task <SourcePoint> EditSourcePointAsync(int[] groupIds, SourcePoint sourcePoint) { try { var previousSourcePoint = await _dbContext.SourcePoints.Include(o => o.Catalog).Include(o => o.Groups).Include(o => o.PublishedHistories).FirstOrDefaultAsync(o => o.Id == sourcePoint.Id); var newGroup = _dbContext.SourcePointGroups.Where(o => groupIds.Contains(o.Id)); var previousValue = new SourcePoint() { Name = sourcePoint.Name, Position = sourcePoint.Position, RangeId = sourcePoint.RangeId, Value = sourcePoint.Value }; if (previousSourcePoint != null) { previousSourcePoint.Name = sourcePoint.Name; previousSourcePoint.Position = sourcePoint.Position; previousSourcePoint.RangeId = sourcePoint.RangeId; previousSourcePoint.Value = sourcePoint.Value; previousSourcePoint.NamePosition = sourcePoint.NamePosition; previousSourcePoint.NameRangeId = sourcePoint.NameRangeId; //var newGroups = groupIds.Select(o => new SourcePointGroup() { Id = o }); var newGroups = _dbContext.SourcePointGroups.Where(o => groupIds.Contains(o.Id)); var deletingGroups = previousSourcePoint.Groups.Except(newGroups, new Comparer <SourcePointGroup>((x, y) => x.Id == y.Id)).ToList(); var addingCourses = newGroups.AsEnumerable().Except(previousSourcePoint.Groups, new Comparer <SourcePointGroup>((x, y) => x.Id == y.Id)); foreach (var group in deletingGroups) { previousSourcePoint.Groups.Remove(group); } foreach (var group in addingCourses) { if (_dbContext.Entry(group).State == EntityState.Detached) { _dbContext.SourcePointGroups.Attach(group); } previousSourcePoint.Groups.Add(group); } } await _dbContext.SaveChangesAsync(); await _logService.WriteLogAsync(new LogEntity() { LogId = "10002", Action = Constant.ACTIONTYPE_EDIT, PointType = Constant.POINTTYPE_SOURCEPOINT, ActionType = ActionTypeEnum.AuditLog, Message = $"Edit source point by {_userProfileService.GetCurrentUser().Username} Previous value: source point named: {previousSourcePoint.Name} in the location: {previousSourcePoint.Position} value: {previousSourcePoint.Value} in the excel file named: {previousSourcePoint.Catalog.FileName} " + $"Current value: source point named: {sourcePoint.Name} in the location {sourcePoint.Position} value: {sourcePoint.Value} in the excel file name: {previousSourcePoint.Catalog.FileName}" }); return(previousSourcePoint); } catch (Exception ex) { var logEntity = new LogEntity() { LogId = "10008", Action = Constant.ACTIONTYPE_EDIT, ActionType = ActionTypeEnum.ErrorLog, PointType = Constant.POINTTYPE_SOURCEPOINT, Message = ".Net Error", Detail = ex.ToString() }; logEntity.Subject = $"{logEntity.LogId} - {logEntity.Action} - {logEntity.PointType} - Error"; await _logService.WriteLogAsync(logEntity); throw ex; } }