hanyadikhetvan-dotnethf/HanyadikHetVan/Services/WeeklyTimeSpanService.cs
Torma Kristóf 0bba2d2faf
Some checks failed
continuous-integration/drone/push Build is failing
refactor db access
2021-05-18 00:53:29 +02:00

81 lines
2.6 KiB
C#

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<WeeklyTimeSpan> 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<Pause> GetPausesOfWeeklyTimeSpan(int weeklytimespanId)
{
return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefault().Pauses;
}
public IEnumerable<WeeklyTimeSpan> GetWeeklyTimeSpanByStartdate(DateTime startTime)
{
return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Startdate.Date.Equals(startTime.Date)).ToList();
}
public async Task<WeeklyTimeSpan> 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;
}
}
}
}