hanyadikhetvan-dotnethf/HanyadikHetVan/Services/PauseService.cs

66 lines
1.7 KiB
C#
Raw Normal View History

2021-05-18 00:53:29 +02:00
using HanyadikHetVan.Data;
using HanyadikHetVan.Data.Entities;
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 PauseService
{
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 PauseService(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<Pause> GetAllPauses()
{
2021-05-18 00:53:29 +02:00
return _dbContext.Pauses.Include(x => x.WeeklyTimeSpan).ToList();
2021-05-03 18:15:26 +02:00
}
public async Task<Pause> AddPause(Pause pause)
{
2021-05-18 00:53:29 +02:00
var obj = await _dbContext.Pauses.AddAsync(pause);
_dbContext.SaveChanges();
return obj.Entity;
2021-05-03 18:15:26 +02:00
}
public bool DeletePause(int pauseId)
{
try
{
2021-05-18 00:53:29 +02:00
var item = _dbContext.Pauses.Where(x => x.Id == pauseId).FirstOrDefault();
_dbContext.Pauses.Remove(item);
_dbContext.SaveChanges();
2021-05-03 18:15:26 +02:00
return true;
}
catch (Exception)
{
return false;
}
}
public bool UpdatePause(Pause pause)
{
try
{
2021-05-18 00:53:29 +02:00
var DataList = _dbContext.Pauses.ToList();
2021-05-03 18:15:26 +02:00
foreach (var item in DataList)
{
2021-05-18 00:53:29 +02:00
_dbContext.Pauses.Update(item);
_dbContext.SaveChanges();
2021-05-03 18:15:26 +02:00
}
return true;
}
catch (Exception)
{
return true;
}
}
}
}