hanyadikhetvan-dotnethf/HanyadikHetVan/Services/WeeklyTimeSpanService.cs

83 lines
3.4 KiB
C#
Raw Normal View History

2021-05-19 04:33:53 +02:00
using AutoMapper;
using AutoMapper.QueryableExtensions;
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-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-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 WeeklyTimeSpanService(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<WeeklyTimeSpanDTO>> GetAllWeeklyTimeSpans()
2021-05-03 18:15:26 +02:00
{
2021-05-19 04:33:53 +02:00
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).ToListAsync();
return _mapper.Map<List<WeeklyTimeSpan>, List<WeeklyTimeSpanDTO>>(wts);
2021-05-03 18:15:26 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<WeeklyTimeSpanDTO> GetWeeklyTimeSpan(int weeklytimespanId)
2021-05-17 22:53:27 +02:00
{
2021-05-19 04:33:53 +02:00
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
return _mapper.Map<WeeklyTimeSpan, WeeklyTimeSpanDTO>(wts);
2021-05-17 22:53:27 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<List<PauseDTO>> GetPausesOfWeeklyTimeSpan(int weeklytimespanId)
2021-05-17 22:53:27 +02:00
{
2021-05-19 04:33:53 +02:00
var pauses = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
return _mapper.Map<ICollection<Pause>, List<PauseDTO>>(pauses.Pauses);
2021-05-17 22:53:27 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<WeeklyTimeSpanDTO> GetWeeklyTimeSpanByStartdate(DateTime startTime)
2021-05-17 22:53:27 +02:00
{
2021-05-19 04:33:53 +02:00
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Startdate.Date.Equals(startTime.Date)).ToListAsync();
return _mapper.Map<List<WeeklyTimeSpan>, WeeklyTimeSpanDTO>(wts);
2021-05-17 22:53:27 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<WeeklyTimeSpanDTO> AddWeeklyTimeSpan(WeeklyTimeSpanDTO weeklytimespan)
2021-05-03 18:15:26 +02:00
{
2021-05-19 04:33:53 +02:00
var wts = _mapper.Map<WeeklyTimeSpanDTO, WeeklyTimeSpan>(weeklytimespan);
var obj = await _dbContext.WeeklyTimeSpans.AddAsync(wts);
await _dbContext.SaveChangesAsync();
return _mapper.Map<WeeklyTimeSpan, WeeklyTimeSpanDTO>(obj.Entity);
2021-05-03 18:15:26 +02:00
}
2021-05-19 04:33:53 +02:00
public async Task<bool> DeleteWeeklyTimeSpan(int weeklytimespanId)
2021-05-03 18:15:26 +02:00
{
try
{
2021-05-19 04:33:53 +02:00
var item = await _dbContext.WeeklyTimeSpans.Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
2021-05-18 00:53:29 +02:00
_dbContext.WeeklyTimeSpans.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<WeeklyTimeSpanDTO> UpdateWeeklyTimeSpan(WeeklyTimeSpanDTO weeklytimespan)
2021-05-03 18:15:26 +02:00
{
2021-05-19 04:33:53 +02:00
var wts = _mapper.Map<WeeklyTimeSpanDTO, WeeklyTimeSpan>(weeklytimespan);
wts.Id = weeklytimespan.Id;
_dbContext.WeeklyTimeSpans.Update(wts);
await _dbContext.SaveChangesAsync();
var newwts = await _dbContext.WeeklyTimeSpans.Where(x => x.Id == weeklytimespan.Id).FirstOrDefaultAsync();
return _mapper.Map<WeeklyTimeSpan, WeeklyTimeSpanDTO>(newwts);
2021-05-03 18:15:26 +02:00
}
}
}