public async Task <ActionResult <int> > RemoveOldestRecords(int n) { // error input if (n <= 0) { return(0); } var result = from r in context.LoadDatas orderby r.Id select r; var count = result.Count(); // large { n }, delete all if (n >= count) { context.LoadDatas.RemoveRange(context.LoadDatas); await context.SaveChangesAsync(); return(count); } // delete oldest { n } value context.LoadDatas.RemoveRange(result.Take(n)); await context.SaveChangesAsync(); return(n); }
public async Task <ActionResult <LoadData> > PostReportLoadData(ReportLoadData data) { if (data.LoadValue < 0 || data.LoadValue > 100) { return(BadRequest()); } // store as a new LoadData. var loadData = new LoadData { LoadValue = data.LoadValue, Timestamp = DateTime.Now }; context.LoadDatas.Add(loadData); await context.SaveChangesAsync(); return(loadData); }