hanyadikhetvan-dotnethf/HanyadikHetVan/Services/PurseService.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2021-05-19 04:33:53 +02:00
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);
}
}
}