hanyadikhetvan-dotnethf/HanyadikHetVan/Services/WeeklyTimeSpanService.cs

81 lines
2.6 KiB
C#
Raw Normal View History

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