| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using BulkPrintingAPI.Configuration;
- using BulkPrintingAPI.Pagination;
- using MAX.Models;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Caching.Memory;
- using Microsoft.Extensions.Logging;
- using System;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Threading.Tasks;
- namespace BulkPrintingAPI.Controllers
- {
- [Produces("application/json")]
- [Route("api/[controller]")]
- public class BatchesController : Controller
- {
- public class OrderRequest
- {
- [Required]
- public int? ProductId { get; set; }
- [Required]
- [Range(1, int.MaxValue)]
- public int Quantity { get; set; }
- [Required]
- public string CustomerReference { get; set; }
- [Required]
- public string InternalReference { get; set; }
- public Guid? OrderGuid { get; set; }
- };
- private readonly ILogger _logger;
- private readonly IMemoryCache _cache;
- private readonly DataEncryptionOptions _dataEncryptionOptions;
- private readonly SFTPOptions _sftpOptions;
- private readonly MAX.ClientFactory _clientFactory;
- private readonly MAXContext _context;
- public BatchesController(ILoggerFactory loggerFactory, IMemoryCache cache,
- DataEncryptionOptions dataEncryptionOptions, SFTPOptions sftpOptions,
- MAX.ClientFactory clientFactory, MAXContext context)
- {
- _logger = loggerFactory.CreateLogger(GetType().FullName);
- _cache = cache;
- _dataEncryptionOptions = dataEncryptionOptions;
- _sftpOptions = sftpOptions;
- _clientFactory = clientFactory;
- _context = context;
- }
- private IQueryable<Batch> BatchesForVendor(int vendorId)
- {
- return _context.Batches.Where(b => b.VendorId == vendorId);
- }
- [HttpGet]
- public async Task<Page<Batch>> GetBatchesAsync([FromQuery] int page = 1,
- [FromQuery] int pageSize = 100, [FromQuery] int? lastBatchId = null, int? minBatchId = null)
- {
- var credentials = await Utils.GetLoginCredentialsFromRequestAsync(HttpContext, _context);
- var query = BatchesForVendor(credentials.Vendor.Id);
- if (minBatchId.HasValue)
- {
- query = query.Where(b => b.Id >= minBatchId.Value).OrderBy(b => b.Id);
- }
- else if (lastBatchId.HasValue)
- {
- // Deprecated in favour of minBatchId for clarity. Use minBatchId = lastBatchId + 1 for equivalent
- // functionality
- query = query.Where(b => b.Id > lastBatchId.Value).OrderBy(b => b.Id);
- }
- else
- {
- query = query.OrderByDescending(b => b.Id);
- }
- return await Page<Batch>.GetPageAsync(query, page, pageSize);
- }
- [HttpGet("{id}")]
- public async Task<IActionResult> GetBatchAsync([FromRoute] int id)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- var credentials = await Utils.GetLoginCredentialsFromRequestAsync(HttpContext, _context);
- var batch = await BatchesForVendor(credentials.Vendor.Id)
- .Include(b => b.Account)
- .SingleOrDefaultAsync(m => m.Id == id);
- if (batch == null)
- {
- return NotFound();
- }
- if (!batch.ReadyForDownload)
- {
- try
- {
- await Utils.DownloadVouchersAsync(_sftpOptions, _context, _logger, batch);
- }
- catch (Exception e)
- {
- _logger.LogError(string.Format(
- "Failed to download vouchers for {0} batchId={1}: {2}",
- credentials.ToString(), batch.Id, e.Message));
- }
- }
- return Ok(batch);
- }
- [HttpPost]
- public async Task<IActionResult> PlaceOrderAsync([FromBody] OrderRequest order)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- if (order.OrderGuid.HasValue)
- {
- var batch = await _context.Batches.SingleOrDefaultAsync(b => b.OrderGuid == order.OrderGuid.Value);
- if (batch != null)
- {
- return BadRequest(new { error = "Duplicate OrderGuid" });
- }
- }
- var credentials = await Utils.GetLoginCredentialsFromRequestAsync(HttpContext, _context);
- var catalogue = await Utils.GetProductCatalogueAsync(_clientFactory, _logger, _cache,
- credentials, false);
- Product product;
- if (!catalogue.ProductMap.TryGetValue(order.ProductId.Value, out product))
- {
- return BadRequest(new { error = "Invalid product ID" });
- }
- var orderResponse = await MAX.Utils.PlaceOrderAsync(_clientFactory, _logger,
- credentials, product, order.Quantity, order.CustomerReference, order.InternalReference, order.OrderGuid,
- Utils.AesDecryptBytes(credentials.Vendor.EncryptedVoucherKey,
- _dataEncryptionOptions.DefaultKey));
- _context.Batches.Add(orderResponse.Batch);
- credentials.User.Account.Balance = orderResponse.RemainingBalance;
- await _context.SaveChangesAsync();
- _logger.LogDebug("Saved batchId={0} for {1}", orderResponse.Batch.Id, credentials.ToString());
- try
- {
- await Utils.DownloadVouchersAsync(_sftpOptions, _context, _logger, orderResponse.Batch);
- }
- catch (Exception e)
- {
- _logger.LogError(string.Format(
- "Failed to download vouchers for {0} batchId={1}: {2}",
- credentials.ToString(), orderResponse.Batch.Id, e.Message));
- }
- return Ok(orderResponse);
- }
- }
- }
|