99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Nevyn.Models;
|
|
|
|
namespace Nevyn.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class MoneyController : ControllerBase
|
|
{
|
|
private readonly MoneyContext _context;
|
|
|
|
public MoneyController(MoneyContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/ShoppingLists
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<WalletDTO>>> GetWallet()
|
|
{
|
|
return await _context.UpdateWallet.Select(x => ItemToDTO(x)).ToListAsync();
|
|
}
|
|
|
|
// GET: api/ShoppingLists/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<WalletDTO>> GetWallet(long id)
|
|
{
|
|
var wallet = await _context.UpdateWallet.FindAsync(id);
|
|
|
|
if (wallet == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return ItemToDTO(wallet);
|
|
}
|
|
|
|
// PUT: api/ShoppingLists/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut]
|
|
public async Task<IActionResult> PutWallet(long id, Wallet wallet)
|
|
{
|
|
|
|
var currentWallet = await _context.UpdateWallet.FindAsync(wallet.Id);
|
|
if (currentWallet == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
currentWallet.Kort = wallet.Kort;
|
|
currentWallet.Buffert = wallet.Buffert;
|
|
currentWallet.Fonder = wallet.Fonder;
|
|
currentWallet.Spara = wallet.Spara;
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/ShoppingLists
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<WalletDTO>> PostWallet(Wallet inputWallet)
|
|
{
|
|
|
|
var wallet = new Wallet
|
|
{
|
|
Buffert = inputWallet.Buffert,
|
|
Fonder = inputWallet.Fonder,
|
|
Kort = inputWallet.Kort,
|
|
Spara = inputWallet.Spara,
|
|
};
|
|
|
|
_context.UpdateWallet.Add(wallet);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction(
|
|
nameof(GetWallet),
|
|
new { id = wallet.Id },
|
|
ItemToDTO(wallet));
|
|
}
|
|
|
|
private static WalletDTO ItemToDTO(Wallet wallet) =>
|
|
new WalletDTO
|
|
{
|
|
Kort = wallet.Kort,
|
|
TotalAmount = wallet.TotalAmount,
|
|
AmountLeftPerDay = wallet.MoneyUntilSalary,
|
|
Debug = "There is " + wallet.DaysLeftUntilSalary + " days left",
|
|
};
|
|
}
|
|
}
|