127 lines
3.8 KiB
C#
127 lines
3.8 KiB
C#
using Accounting.Data;
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
namespace Accounting.Controllers;
|
|
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class AccountingController : ControllerBase
|
|
{
|
|
private readonly DataContext _context;
|
|
|
|
public AccountingController(DataContext dataContext)
|
|
{
|
|
_context = dataContext;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<AccountRecord>>> GetAllAccRecords()
|
|
{
|
|
|
|
var accRecords = await _context.AccountRecords.ToListAsync();
|
|
|
|
return Ok(accRecords);
|
|
|
|
|
|
|
|
//var accRecords = new List<AccountRecord>{
|
|
// new AccountRecord {
|
|
// Id = 1,
|
|
// Avisering = "Efaktura",
|
|
// Belopp = 250.50D,
|
|
// BetalDatum = DateTime.Parse("2024-05-30"),
|
|
// Konto = "1234-5678",
|
|
// Mottagare = "Tele2",
|
|
// Stored = DateTime.Now
|
|
// }
|
|
//};
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<AccountRecord>> GetAccountRecord(int id)
|
|
{
|
|
var accRecord = await _context.AccountRecords.FindAsync(id);
|
|
if (accRecord is null)
|
|
return NotFound("AccountRecord not found");
|
|
|
|
return Ok(accRecord);
|
|
}
|
|
|
|
[HttpPost("Addrecord")]
|
|
public async Task<ActionResult<List<AccountRecord>>> AddAccRecord(AccountRecord accRecord)
|
|
{
|
|
_context.AccountRecords.Add(accRecord);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(await _context.AccountRecords.ToListAsync());
|
|
}
|
|
|
|
|
|
[HttpPost("AddAccRecordsFromJson/{jsonFile}")]
|
|
public async Task<ActionResult<List<AccountRecord>>> AddAccRecordsFromJson(string jsonFile)
|
|
{
|
|
var jsonText = await System.IO.File.ReadAllTextAsync(jsonFile);
|
|
|
|
var accRecords = JsonConvert.DeserializeObject<List<AccountRecord>>(jsonText);
|
|
|
|
if (accRecords == null)
|
|
return BadRequest($"File not found : {jsonText}");
|
|
|
|
|
|
using (var transaction = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
await _context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.AccountRecords ON");
|
|
await _context.AccountRecords.AddRangeAsync(accRecords);
|
|
await _context.SaveChangesAsync();
|
|
|
|
await _context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.AccountRecords OFF");
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
return BadRequest($"Error in call {ex.Message}");
|
|
}
|
|
|
|
}
|
|
return Ok(await _context.AccountRecords.ToListAsync());
|
|
}
|
|
|
|
|
|
|
|
[HttpPut]
|
|
public async Task<ActionResult<List<AccountRecord>>> UpdateAccRecord(AccountRecord updatedAccRecord)
|
|
{
|
|
var accRecord = await _context.AccountRecords.FindAsync(updatedAccRecord.Id);
|
|
if (accRecord is null)
|
|
return NotFound("Record not found");
|
|
|
|
accRecord.Stored = updatedAccRecord.Stored;
|
|
accRecord.BetalDatum = updatedAccRecord.BetalDatum;
|
|
accRecord.Avisering = updatedAccRecord.Avisering;
|
|
accRecord.Mottagare = updatedAccRecord.Mottagare;
|
|
accRecord.Belopp = updatedAccRecord.Belopp;
|
|
accRecord.Konto = updatedAccRecord.Konto;
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(await _context.AccountRecords.ToListAsync());
|
|
}
|
|
|
|
[HttpDelete]
|
|
public async Task<ActionResult<List<AccountRecord>>> DeleteAccRecord(int id)
|
|
{
|
|
var AccRecord = await _context.AccountRecords.FindAsync(id);
|
|
if (AccRecord is null)
|
|
return NotFound("AccRecord not found");
|
|
|
|
_context.AccountRecords.Remove(AccRecord);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(await _context.AccountRecords.ToListAsync());
|
|
}
|
|
}
|