This commit is contained in:
35
HanyadikHetVan/Services/FunFactService.cs
Normal file
35
HanyadikHetVan/Services/FunFactService.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using AutoMapper;
|
||||
using HanyadikHetVan.Data;
|
||||
using HanyadikHetVan.Data.Entities;
|
||||
using HanyadikHetVan.DTO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HanyadikHetVan.Services
|
||||
{
|
||||
public class FunFactService
|
||||
{
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public FunFactService(ApplicationDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
|
||||
public async Task<FunFactDTO> GetFunFactOfWeeklyTimeSpan(int weeklytimespanId)
|
||||
{
|
||||
var funfact = await _dbContext.FunFacts.Where(x => x.WeeklyTimeSpanId == weeklytimespanId).FirstOrDefaultAsync();
|
||||
return _mapper.Map<FunFact, FunFactDTO>(funfact);
|
||||
}
|
||||
|
||||
public async Task<double> GetFunFactorOfFunFact(int funfactId)
|
||||
{
|
||||
var funfact = await _dbContext.FunFacts.Where(x => x.Id == funfactId).FirstOrDefaultAsync();
|
||||
return funfact.FunFactor;
|
||||
}
|
||||
}
|
||||
}
|
@ -27,6 +27,13 @@ namespace HanyadikHetVan.Services
|
||||
return latestDto.CurrentWeek;
|
||||
}
|
||||
|
||||
public async Task<int> GetUserDefaultWeek(string userId)
|
||||
{
|
||||
var latest = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.UserId == userId).LastAsync();
|
||||
var latestDto = _mapper.Map<WeeklyTimeSpan, WeeklyTimeSpanDTO>(latest);
|
||||
return latestDto.CurrentWeek;
|
||||
}
|
||||
|
||||
public async Task<int> GetWeekById(int weeklytimespanId)
|
||||
{
|
||||
var wts = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefaultAsync();
|
||||
|
26
HanyadikHetVan/Services/IdentityService.cs
Normal file
26
HanyadikHetVan/Services/IdentityService.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HanyadikHetVan.Services
|
||||
{
|
||||
public class IdentityService
|
||||
{
|
||||
private readonly IHttpContextAccessor httpContextAccessor;
|
||||
|
||||
public IdentityService(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
this.httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public string GetUserId()
|
||||
{
|
||||
return
|
||||
httpContextAccessor.HttpContext.User.Claims
|
||||
.SingleOrDefault(x => x.Type.ToLower() == "sub")
|
||||
?.Value ?? throw new Exception();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
using AutoMapper;
|
||||
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 PurseService
|
||||
{
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public PurseService(ApplicationDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
|
||||
public async Task<PurseDTO> GetBalanceOfUser(String userId)
|
||||
{
|
||||
var purse = await _dbContext.Purses.Where(x => x.UserId == userId).FirstOrDefaultAsync();
|
||||
return _mapper.Map<Purse, PurseDTO>(purse);
|
||||
}
|
||||
|
||||
public async Task<PurseDTO> SetBalanceOfUser(String userId, int newBalance)
|
||||
{
|
||||
var purse = new Purse() { UserId = userId, Balance = newBalance };
|
||||
_dbContext.Purses.Update(purse);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
var newPurse = await _dbContext.Purses.Where(x => x.UserId == userId).FirstOrDefaultAsync();
|
||||
return _mapper.Map<Purse, PurseDTO>(newPurse);
|
||||
}
|
||||
}
|
||||
}
|
@ -46,9 +46,10 @@ namespace HanyadikHetVan.Services
|
||||
return _mapper.Map<List<WeeklyTimeSpan>, WeeklyTimeSpanDTO>(wts);
|
||||
}
|
||||
|
||||
public async Task<WeeklyTimeSpanDTO> AddWeeklyTimeSpan(WeeklyTimeSpanDTO weeklytimespan)
|
||||
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);
|
||||
|
Reference in New Issue
Block a user