// GET: api/Sales public IEnumerable <Sales> Get() { DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.OrderBy(i => i.Date); return(result); }
// DELETE: api/Sales/5 public void Delete(Guid?id) { DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.Where(i => i.Id == id).FirstOrDefault(); context.Sales.Remove(result); context.SaveChanges(); }
// POST: api/Sales public Sales Post([FromBody] Sales obj) { DatabaseTaskEntities context = new DatabaseTaskEntities(); context.Sales.Add(new Sales { Id = Guid.NewGuid(), Date = obj.Date, Price = obj.Price }); context.SaveChanges(); return(obj); }
// GET: api/SalesPerDays public IQueryable <StructDays> Get(DateTime StartDate, DateTime EndDate) { DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.GroupBy(i => i.Date) .Select(grp => new StructDays() { Date = grp.Key, Price = grp.Sum(p => p.Price), CountPrice = grp.Count() }) .Where(p => p.Date >= StartDate && p.Date <= EndDate); return(result); }
// GET: api/SalesPerYears public IQueryable <StructYears> Get(DateTime StartDate, DateTime EndDate) { int StartYear = StartDate.Year; int EndYear = EndDate.Year; DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.GroupBy(i => SqlFunctions.DatePart("year", i.Date)) .Select(grp => new StructYears() { NumberYear = grp.Key.Value, CountPrice = grp.Count(), Price = grp.Sum(q => q.Price) }) .Where(q => q.NumberYear >= StartYear && q.NumberYear <= EndYear); return(result); }
// PUT: api/Sales/5 public void Put(Guid?id, [FromBody] Sales obj) { DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.Where(i => i.Id == id).FirstOrDefault(); if (obj.Price != null) { result.Price = obj.Price; } if (obj.Date != null) { result.Date = obj.Date; } context.SaveChanges(); }
// GET: api/SalesPerQuarters public IQueryable <StructQuarters> Get(DateTime StartDate, DateTime EndDate) { int startYear = StartDate.Year; int endYear = EndDate.Year; DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.Select(i => new Sale() { Id = i.Id, Price = i.Price, Date = i.Date }) .Where(i => i.Date >= StartDate && i.Date <= EndDate); var fimleResult = result.GroupBy(i => new { QuarterNumber = SqlFunctions.DatePart("quarter", i.Date), YearNumber = SqlFunctions.DatePart("year", i.Date) }) .Select(grp => new StructQuarters() { NumberQuarter = grp.Key.QuarterNumber, NumberYear = grp.Key.YearNumber, Price = grp.Sum(i => i.Price), CountPrice = grp.Count() }); return(fimleResult); }
// GET: api/SalesPerWeeks public IQueryable <StructWeeks> Get(DateTime StartDate, DateTime EndDate) { int startMonth = StartDate.Month; int endMonth = EndDate.Month; int startYear = StartDate.Year; int endYear = EndDate.Year; DatabaseTaskEntities context = new DatabaseTaskEntities(); var result = context.Sales.Select(i => new Sale() { Id = i.Id, Date = i.Date, Price = i.Price }) .Where(q => q.Date >= StartDate && q.Date <= EndDate).OrderBy(i => i.Date); var finleResult = result.GroupBy(i => new { WeekNumber = SqlFunctions.DatePart("week", i.Date), MonthNumber = SqlFunctions.DatePart("month", i.Date), YearNumber = SqlFunctions.DatePart("year", i.Date) }) .Select(grp => new StructWeeks() { NumberWeek = grp.Key.WeekNumber, NumberYear = grp.Key.YearNumber, NumberMonth = grp.Key.MonthNumber, CountPrice = grp.Count(), Price = grp.Sum(i => i.Price) }); return(finleResult); }