using HanyadikHetVan.Data; using HanyadikHetVan.Data.Entities; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace HanyadikHetVan.Services { public class WeeklyTimeSpanService { private readonly ApplicationDbContext _dbContext; public WeeklyTimeSpanService(ApplicationDbContext dbContext) { _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); } public IEnumerable GetAllWeeklyTimeSpans() { return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).ToList(); } public WeeklyTimeSpan GetWeeklyTimeSpan(int weeklytimespanId) { return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefault(); } public ICollection GetPausesOfWeeklyTimeSpan(int weeklytimespanId) { return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefault().Pauses; } public IEnumerable GetWeeklyTimeSpanByStartdate(DateTime startTime) { return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Startdate.Date.Equals(startTime.Date)).ToList(); } public async Task AddWeeklyTimeSpan(WeeklyTimeSpan weeklytimespan) { var obj = await _dbContext.WeeklyTimeSpans.AddAsync(weeklytimespan); _dbContext.SaveChanges(); return obj.Entity; } public bool DeleteWeeklyTimeSpan(int weeklytimespanId) { try { var item = _dbContext.WeeklyTimeSpans.Where(x => x.Id == weeklytimespanId).FirstOrDefault(); _dbContext.WeeklyTimeSpans.Remove(item); _dbContext.SaveChanges(); return true; } catch (Exception) { return false; } } public bool UpdateWeeklyTimeSpan(WeeklyTimeSpan weeklytimespan) { try { var DataList = _dbContext.WeeklyTimeSpans.ToList(); foreach (var item in DataList) { _dbContext.WeeklyTimeSpans.Update(item); _dbContext.SaveChanges(); } return true; } catch (Exception) { return true; } } } }