304 lines
12 KiB
C#
304 lines
12 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using RMuseum.Models.Accounting;
|
|
using RMuseum.Models.Accounting.ViewModels;
|
|
using RMuseum.Models.Auth.Memory;
|
|
using RMuseum.Services;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace RMuseum.Controllers
|
|
{
|
|
[Produces("application/json")]
|
|
[Route("api/donations")]
|
|
public class DonationAccountingController : Controller
|
|
{
|
|
/// <summary>
|
|
/// returns all donations
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorDonationViewModel[]))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
public async Task<IActionResult> GetDonations()
|
|
{
|
|
try
|
|
{
|
|
var res = await _donationService.GetDonations();
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// add donation + regenerate donations page
|
|
/// </summary>
|
|
/// <param name="donation"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorDonationViewModel))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> AddDonation([FromBody] GanjoorDonationViewModel donation)
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.AddDonation(userId, donation);
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// update donation date and donorname + regenerate donations page
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="donation"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> UpdateDonation(int id, [FromBody] UpdateDateDescriptionViewModel donation)
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.UpdateDonation(userId, id, donation);
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// delete donation + regenerate donations page
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> DeleteDonation(int id)
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.DeleteDonation(userId, id);
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// returns all expenses
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("expenses")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorExpense[]))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
public async Task<IActionResult> GetExpenses()
|
|
{
|
|
try
|
|
{
|
|
var res = await _donationService.GetExpenses();
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// add expense + regenerate donations page
|
|
/// </summary>
|
|
/// <param name="expense"></param>
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("expense")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorDonationViewModel))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> AddExpense([FromBody] GanjoorExpense expense)
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.AddExpense(userId, expense);
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// update expense date and description + regenerate donations page
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="expense"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("expense/{id}")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> UpdateExpense(int id, [FromBody] UpdateDateDescriptionViewModel expense)
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.UpdateExpense(userId, id, expense);
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// delete expense
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("expense/{id}")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> DeleteExpense(int id)
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.DeleteExpense(userId, id);
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok(res.Result);
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// one time import
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost("onetimeimport")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> Import()
|
|
{
|
|
try
|
|
{
|
|
var res = await _donationService.InitializeRecords();
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok();
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// regenerate donations page
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPut("page")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public async Task<IActionResult> RegenerateDonationsPage()
|
|
{
|
|
try
|
|
{
|
|
Guid userId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
|
|
|
|
var res = await _donationService.RegenerateDonationsPage(userId, "بازسازی دستی صفحهٔ کمکهای مالی");
|
|
if (!string.IsNullOrEmpty(res.ExceptionString))
|
|
return BadRequest(res.ExceptionString);
|
|
return Ok();
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
return BadRequest(exp.ToString());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// is account info settings is on or off (for deciding to regenerate donations page based on it)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("accountinfo/visible")]
|
|
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
|
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
|
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
|
|
public IActionResult IsAccountInfoVisible()
|
|
{
|
|
return Ok(_donationService.ShowAccountInfo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// donation service
|
|
/// </summary>
|
|
private readonly IDonationService _donationService;
|
|
|
|
public DonationAccountingController(IDonationService donationService)
|
|
{
|
|
_donationService = donationService;
|
|
}
|
|
}
|
|
}
|