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 GetBalanceOfUser(String userId) { var purse = await _dbContext.Purses.Where(x => x.UserId == userId).FirstOrDefaultAsync(); return _mapper.Map(purse); } public async Task 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(newPurse); } } }