Example #1
0
        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));
        }
Example #2
0
        public IHttpActionResult Delete2(string id, [FromBody] DeleteBody body)
        {
            if (string.IsNullOrEmpty(id))
            {
                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.SimpleDeletion);

            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, "Routine not found, may already deleted, please refresh page"));
            }

            fulfill.IsDeleted    = true;
            fulfill.DeleteAt     = DateTime.Now;
            fulfill.DeletedBy    = "fixme-delete";
            fulfill.DeleteReason = body.Reason;

            WriteToFile(fulfillmentPath, fulfillments);
            perf.End("override fulfill end", true);
            return(DtoResultV5.Success(Json, DtoRoutine.From(fulfill, false)));
        }
Example #3
0
        public IHttpActionResult PutRecursive(string id, [FromBody] PutRecursiveBody body)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }
            if (body.Enable && (body.IntervalDays ?? 0) < 1)
            {
                throw new ArgumentException($"invalid recursive interval {body.IntervalDays}");
            }

            var perf            = PerfCounter.NewThenCheck(this.ToString() + "." + MethodBase.GetCurrentMethod().Name);
            var fulfillmentPath = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Fulfillments);

            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."));
            }

            fulfill.EnableSchedule = body.Enable;
            if (body.Enable)
            {
                fulfill.RecursiveIntervalDays = body.IntervalDays;
            }
            fulfill.UpdateBy = "fixme-put-rec";
            fulfill.UpdateAt = DateTime.Now;

            WriteToFile(fulfillmentPath, fulfillments);
            perf.End("override fulfill end", true);
            return(DtoResultV5.Success(Json, DtoRoutine.From(fulfill, false)));
        }
Example #4
0
        public IHttpActionResult Get(string id, string history = null)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return(DtoResultV5.Fail(BadRequest, "id is empty"));
            }

            if (Constants.LskArchived.Equals(history, StringComparison.OrdinalIgnoreCase))
            {
                var archivePath        = GetFullIntrospectionDataPath(DateTime.Now, IntrospectionDataType.Archives);
                var archivedRecords    = ReadLskjson <Guid, FulfillmentArchive>(archivePath, CollectLskjsonLineIncludeDeleted);
                var recordsByParentUid = archivedRecords.Where(a => id.Equals(a.ParentUid.ToString(), StringComparison.OrdinalIgnoreCase));
                return(DtoResultV5.Success(Json, recordsByParentUid.Select(r => DtoFulfillmentArchive.From(r))));
            }
            else
            {
                var fulfillments = ReadFulfillmentsOfThisYear(AuthMode.None);
                var fulfillment  = fulfillments.FirstOrDefault(f => id.Equals(f.Uid.ToString(), StringComparison.OrdinalIgnoreCase));
                var dto          = fulfillment == null ? null : DtoRoutine.From(fulfillment, true);
                return(DtoResultV5.Success(Json, dto));
            }
        }
Example #5
0
        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)));
        }