public IHttpActionResult Get() { var fulfillments = ReadFulfillmentsOfThisYear(AuthMode.Simple); //for migrate if (fulfillments.Any(f => !f.HasMigrated || f.LastFulfill.HasValue)) { foreach (var fulfill in fulfillments) { if (!fulfill.HasMigrated) { fulfill.HasMigrated = true; if (fulfill.HistoryFulfillments?.Length > 0) { var migrateUnit = fulfill.HistoryFulfillments.Select(h => new FulfillmentArchive(fulfill.Uid, null, h)); //var archivePath = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Archives); //AppendObjectToFile(archivePath, migrateUnit.ToArray()); fulfill.StagedArchives = migrateUnit.ToArray(); } } if (fulfill.LastFulfill.HasValue) { var record = new FulfillmentArchive(fulfill.Uid, fulfill.LastRemark, fulfill.LastFulfill, fulfill.UpdateBy, fulfill.UpdateAt); if (fulfill.StagedArchives?.Length > 0) { var staged = fulfill.StagedArchives.ToList(); staged.Add(record); fulfill.StagedArchives = staged.ToArray(); } else { fulfill.StagedArchives = new[] { record }; } fulfill.LastFulfill = null; fulfill.LastRemark = null; } } var path = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Fulfillments); WriteToFile(path, fulfillments); } var dtoList = fulfillments.Select(f => DtoRoutine.From(f, false)); return(DtoResultV5.Success(Json, dtoList)); }
public IHttpActionResult DeleteHistoryRecord2(string parentId, string id, [FromBody] DeleteHistoryBody body) { if (string.IsNullOrEmpty(parentId)) { throw new ArgumentNullException(nameof(parentId)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } if (body.Kind != Constants.LskArchived && body.Kind != Constants.LskStaged) { return(DtoResultV5.Fail(BadRequest, $"unsupported deletion: '{body.Kind}'")); } var perf = PerfCounter.NewThenCheck(this.ToString() + "." + MethodBase.GetCurrentMethod().Name); var fulfillmentPath = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Fulfillments); var antiSpamResult = PassAntiSpamDefender(fulfillmentPath, AuthMode.SimpleDeletion); if (!antiSpamResult.Valid) { return(DtoResultV5.Fail(BadRequest, antiSpamResult.Message)); } perf.Check("anti spam end"); SyncRoutine(fulfillmentPath); perf.Check("sync routine end"); var inputParentUid = Guid.Parse(parentId); var inputUid = Guid.Parse(id); string filePath = null; List <ILskjsonLine> allRecords = null; FulfillmentArchive history = null; if (body.Kind == Constants.LskStaged) { filePath = fulfillmentPath; var fulfillments = ReadLskjson <Guid, RoutineFulfillment>(filePath, CollectLskjsonLineIncludeDeleted); var fulfill = fulfillments.FirstOrDefault(f => f.Uid == inputParentUid); if (fulfill == null) { return(DtoResultV5.Fail(BadRequest, "Routine not found, may already deleted, please refresh page")); } if (!fulfill.HasMigrated) { return(DtoResultV5.Fail(BadRequest, "Migrate routine first")); } if (fulfill.StagedArchives == null || fulfill.StagedArchives.Length == 0) { return(DtoResultV5.Fail(BadRequest, "No staged history found, please refresh page")); } allRecords = fulfillments.ToList <ILskjsonLine>(); history = fulfill.StagedArchives.FirstOrDefault(s => s.Uid == inputUid); } else if (body.Kind == Constants.LskArchived) { filePath = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Archives); var historyRecords = ReadLskjson <Guid, FulfillmentArchive>(filePath, CollectLskjsonLineIncludeDeleted); allRecords = historyRecords.ToList <ILskjsonLine>(); history = historyRecords.FirstOrDefault(h => h.ParentUid == inputParentUid && h.Uid == inputUid); } if (history == null) { return(DtoResultV5.Fail(BadRequest, $"No {body.Kind} history matches with provided id, please refresh page")); } history.IsDeleted = true; history.DeleteAt = DateTime.Now; history.DeletedBy = "fixme-del"; history.DeleteReason = body.Reason; WriteToFile(filePath, allRecords); perf.End($"delete {body.Kind} history end", true); return(DtoResultV5.Success(Json, body.Kind)); }
public IHttpActionResult Put(string id, [FromBody] PutBody body) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } var perf = PerfCounter.NewThenCheck(this.ToString() + "." + MethodBase.GetCurrentMethod().Name); var fulfillmentPath = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Fulfillments); var antiSpamResult = PassAntiSpamDefender(fulfillmentPath, AuthMode.None); if (!antiSpamResult.Valid) { return(DtoResultV5.Fail(BadRequest, antiSpamResult.Message)); } perf.Check("anti spam end"); SyncRoutine(fulfillmentPath); perf.Check("sync routine end"); var fulfillments = ReadLskjson <Guid, RoutineFulfillment>(fulfillmentPath, CollectLskjsonLineIncludeDeleted); perf.Check("read fulfillment end"); var inputUid = Guid.Parse(id); var fulfill = fulfillments.FirstOrDefault(f => f.Uid == inputUid); if (fulfill == null) { return(DtoResultV5.Fail(BadRequest, "expired data found, please reload page first.")); } var offsetDays = int.Parse(antiSpamResult.Message); var date = DateTime.Now.AddDays(-1 * Math.Abs(offsetDays)); var record = new FulfillmentArchive(fulfill.Uid, body.LastRemark, date, "fixme-put", DateTime.Now); if (fulfill.StagedArchives == null) { fulfill.StagedArchives = new[] { record }; } else { var staged = fulfill.StagedArchives.ToList(); staged.Add(record); if (staged.Count >= Constants.LskFulfillmentActiveRecords + Constants.LskFulfillmentArchiveUnit) { const int startIndex = 0; var archiveUnit = staged.GetRange(startIndex, Constants.LskFulfillmentArchiveUnit); var archivePath = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Archives); AppendObjectToFile(archivePath, archiveUnit.ToArray()); staged.RemoveRange(startIndex, archiveUnit.Count); fulfill.HasArchived = true; } fulfill.StagedArchives = staged.ToArray(); } //fulfill.StagedArchives = fulfill.StagedArchives.Concat(new[] { record }).ToArray(); fulfill.UpdateBy = "fixme-update"; fulfill.UpdateAt = DateTime.Now; WriteToFile(fulfillmentPath, fulfillments); perf.End("override fulfill end", true); return(DtoResultV5.Success(Json, DtoRoutine.From(fulfill, false))); }