hanyadikhetvan-dotnethf/HanyadikHetVan/Services/PauseService.cs

78 lines
2.7 KiB
C#
Raw Normal View History

2021-05-19 04:33:53 +02:00
using AutoMapper;
using HanyadikHetVan.Data;
2021-05-18 00:53:29 +02:00
using HanyadikHetVan.Data.Entities;
2021-05-19 04:33:53 +02:00
using HanyadikHetVan.DTO;
2021-05-18 00:53:29 +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 PauseService
{
2021-05-18 00:53:29 +02:00
private readonly ApplicationDbContext _dbContext;
2021-05-19 04:33:53 +02:00
private readonly IMapper _mapper;
2021-05-03 18:15:26 +02:00
2021-05-19 04:33:53 +02:00
public PauseService(ApplicationDbContext dbContext, IMapper mapper)
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-19 04:33:53 +02:00
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
2021-05-03 18:15:26 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<List<PauseDTO>> GetAllPauses()
2021-05-03 18:15:26 +02:00
{
2021-05-19 04:33:53 +02:00
var pauses = await _dbContext.Pauses.Include(x => x.WeeklyTimeSpan).ToListAsync();
return _mapper.Map<List<Pause>, List<PauseDTO>>(pauses);
2021-05-03 18:15:26 +02:00
}
2021-05-20 01:36:54 +02:00
public async Task<PauseDTO> AddPause(string userId, PauseDTO pause)
2021-05-03 18:15:26 +02:00
{
2021-05-20 01:36:54 +02:00
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.UserId == userId && x.Id == pause.WeeklyTimeSpanId).FirstOrDefaultAsync();
if (wts == null)
{
throw new Exception("Not found");
}
2021-05-19 04:33:53 +02:00
var pauseEntity = _mapper.Map<PauseDTO, Pause>(pause);
2021-05-20 01:36:54 +02:00
pauseEntity.WeeklyTimeSpanId = pause.WeeklyTimeSpanId;
2021-05-19 04:33:53 +02:00
var obj = _dbContext.Pauses.Add(pauseEntity);
await _dbContext.SaveChangesAsync();
return _mapper.Map<Pause, PauseDTO>(obj.Entity);
2021-05-03 18:15:26 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<PauseDTO> GetPause(int pauseId)
{
var pause = await _dbContext.Pauses.Where(x => x.Id == pauseId).FirstOrDefaultAsync();
return _mapper.Map<Pause, PauseDTO>(pause);
}
public async Task<bool> DeletePause(int pauseId)
2021-05-03 18:15:26 +02:00
{
try
{
2021-05-19 04:33:53 +02:00
var item = await _dbContext.Pauses.Where(x => x.Id == pauseId).FirstOrDefaultAsync();
2021-05-18 00:53:29 +02:00
_dbContext.Pauses.Remove(item);
2021-05-19 04:33:53 +02:00
await _dbContext.SaveChangesAsync();
2021-05-03 18:15:26 +02:00
return true;
}
catch (Exception)
{
return false;
}
}
2021-05-19 04:33:53 +02:00
public async Task<PauseDTO> UpdatePause(PauseDTO pause)
2021-05-03 18:15:26 +02:00
{
2021-05-19 04:33:53 +02:00
var pauseEntity = _mapper.Map<PauseDTO, Pause>(pause);
pauseEntity.Id = pause.Id;
_dbContext.Pauses.Update(pauseEntity);
await _dbContext.SaveChangesAsync();
var newPause = await _dbContext.Pauses.Where(x => x.Id == pause.Id).FirstOrDefaultAsync();
return _mapper.Map<Pause, PauseDTO>(newPause);
2021-05-03 18:15:26 +02:00
}
}
}