hanyadikhetvan-dotnethf/HanyadikHetVan/Services/WeeklyTimeSpanService.cs
Torma Kristóf 10c1bb008f
All checks were successful
continuous-integration/drone/push Build is passing
final
2021-05-20 01:36:54 +02:00

84 lines
3.5 KiB
C#

using AutoMapper;
using AutoMapper.QueryableExtensions;
using HanyadikHetVan.Data;
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.DTO;
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;
private readonly IMapper _mapper;
public WeeklyTimeSpanService(ApplicationDbContext dbContext, IMapper mapper)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public async Task<List<WeeklyTimeSpanDTO>> GetAllWeeklyTimeSpans()
{
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Include(x => x.FunFacts).ToListAsync();
return _mapper.Map<List<WeeklyTimeSpan>, List<WeeklyTimeSpanDTO>>(wts);
}
public async Task<WeeklyTimeSpanDTO> GetWeeklyTimeSpan(int weeklytimespanId)
{
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Include(x => x.FunFacts).Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
return _mapper.Map<WeeklyTimeSpan, WeeklyTimeSpanDTO>(wts);
}
public async Task<List<PauseDTO>> GetPausesOfWeeklyTimeSpan(int weeklytimespanId)
{
var pauses = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Include(x => x.FunFacts).Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
return _mapper.Map<ICollection<Pause>, List<PauseDTO>>(pauses.Pauses);
}
public async Task<WeeklyTimeSpanDTO> GetWeeklyTimeSpanByStartdate(DateTime startTime)
{
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Include(x => x.FunFacts).Where(x => x.Startdate.Date.Equals(startTime.Date)).ToListAsync();
return _mapper.Map<List<WeeklyTimeSpan>, WeeklyTimeSpanDTO>(wts);
}
public async Task<WeeklyTimeSpanDTO> AddWeeklyTimeSpan(string userId, WeeklyTimeSpanDTO weeklytimespan)
{
var wts = _mapper.Map<WeeklyTimeSpanDTO, WeeklyTimeSpan>(weeklytimespan);
wts.UserId = userId;
var obj = await _dbContext.WeeklyTimeSpans.AddAsync(wts);
await _dbContext.SaveChangesAsync();
return _mapper.Map<WeeklyTimeSpan, WeeklyTimeSpanDTO>(obj.Entity);
}
public async Task<bool> DeleteWeeklyTimeSpan(int weeklytimespanId)
{
try
{
var item = await _dbContext.WeeklyTimeSpans.Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
_dbContext.WeeklyTimeSpans.Remove(item);
await _dbContext.SaveChangesAsync();
return true;
}
catch (Exception)
{
return false;
}
}
public async Task<WeeklyTimeSpanDTO> UpdateWeeklyTimeSpan(WeeklyTimeSpanDTO weeklytimespan)
{
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);
}
}
}