Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This file explains how Visual Studio created the project.

The following steps were used to generate this project:
- Create new ASP\.NET Core Web API project.
- Update project file to add a reference to the frontend project and set SPA properties.
- Update `launchSettings.json` to register the SPA proxy as a startup assembly.
- Add project to the startup projects list.
- Write this file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using Grid_PostgreSQL.Server.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Syncfusion.EJ2.Base;
using System.Linq;

namespace Grid_PostgreSQL.Server.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class PurchaseOrderController : ControllerBase
{
private readonly PurchaseOrderDbContext _dbContext;

public PurchaseOrderController(PurchaseOrderDbContext dbContext)
{
_dbContext = dbContext;
}

// READ
[HttpPost("getpurchasedata")]
public IActionResult UrlDataSource([FromBody] DataManagerRequest request)
{
IQueryable<PurchaseOrder> purchaseOrdersQuery = _dbContext.PurchaseOrders.AsNoTracking();
var dataOperations = new DataOperations();

if (request.Search?.Count > 0)
purchaseOrdersQuery = dataOperations.PerformSearching(purchaseOrdersQuery, request.Search)
.Cast<PurchaseOrder>()
.AsQueryable();

if (request.Where?.Count > 0)
purchaseOrdersQuery = dataOperations.PerformFiltering(purchaseOrdersQuery, request.Where, request.Where[0].Operator)
.Cast<PurchaseOrder>()
.AsQueryable();

if (request.Sorted?.Count > 0)
purchaseOrdersQuery = dataOperations.PerformSorting(purchaseOrdersQuery, request.Sorted)
.Cast<PurchaseOrder>()
.AsQueryable();
else
purchaseOrdersQuery = purchaseOrdersQuery.OrderBy(p => p.PurchaseOrderId);

var totalCount = purchaseOrdersQuery.Count();

if (request.Skip > 0)
purchaseOrdersQuery = purchaseOrdersQuery.Skip(request.Skip);

if (request.Take > 0)
purchaseOrdersQuery = purchaseOrdersQuery.Take(request.Take);

return request.RequiresCounts
? Ok(new { result = purchaseOrdersQuery.ToList(), count = totalCount })
: Ok(purchaseOrdersQuery.ToList());
}

// CREATE
[HttpPost("insert")]
public IActionResult Insert([FromBody] CRUDModel<PurchaseOrder> crudRequest)
{
var purchaseOrder = crudRequest.Value;

purchaseOrder.PurchaseOrderId = 0;
purchaseOrder.TotalAmount = purchaseOrder.Quantity * purchaseOrder.UnitPrice;
purchaseOrder.CreatedOn = DateTime.UtcNow;
purchaseOrder.UpdatedOn = DateTime.UtcNow;

_dbContext.PurchaseOrders.Add(purchaseOrder);
_dbContext.SaveChanges();

return Ok(purchaseOrder);
}

// UPDATE
[HttpPost("update")]
public IActionResult Update([FromBody] CRUDModel<PurchaseOrder> crudRequest)
{
var purchaseOrder = crudRequest.Value;

purchaseOrder.TotalAmount = purchaseOrder.Quantity * purchaseOrder.UnitPrice;
purchaseOrder.UpdatedOn = DateTime.UtcNow;

_dbContext.Entry(purchaseOrder).State = EntityState.Modified;
_dbContext.SaveChanges();

return Ok(purchaseOrder);
}

// DELETE
[HttpPost("remove")]
public IActionResult Remove([FromBody] CRUDModel<PurchaseOrder> crudRequest)
{
// Safely parse the key
var purchaseOrderId = int.Parse(crudRequest.Key.ToString());

var existingOrder = _dbContext.PurchaseOrders
.FirstOrDefault(p => p.PurchaseOrderId == purchaseOrderId);

if (existingOrder != null)
{
_dbContext.PurchaseOrders.Remove(existingOrder);
_dbContext.SaveChanges();
}

return Ok(crudRequest);
}

// BATCH
[HttpPost("batch")]
public IActionResult Batch([FromBody] CRUDModel<PurchaseOrder> crudRequest)
{
// Handle updated records
if (crudRequest.Changed != null)
{
foreach (var purchaseOrder in crudRequest.Changed)
{
purchaseOrder.TotalAmount = purchaseOrder.Quantity * purchaseOrder.UnitPrice;
purchaseOrder.UpdatedOn = DateTime.UtcNow; // safe with timestamp with time zone
_dbContext.PurchaseOrders.Attach(purchaseOrder);
_dbContext.Entry(purchaseOrder).State = EntityState.Modified;
}
}

// Handle newly added records
if (crudRequest.Added != null)
{
foreach (var purchaseOrder in crudRequest.Added)
{
purchaseOrder.PurchaseOrderId = 0;
purchaseOrder.TotalAmount = purchaseOrder.Quantity * purchaseOrder.UnitPrice;
purchaseOrder.UpdatedOn = DateTime.UtcNow;
_dbContext.PurchaseOrders.Add(purchaseOrder);
}
}

// Handle deleted records
if (crudRequest.Deleted != null)
{
foreach (var deletedItem in crudRequest.Deleted)
{
// Safely parse the key if it comes as JsonElement
var idString = deletedItem.PurchaseOrderId.ToString();
if (int.TryParse(idString, out var purchaseOrderId))
{
var existingOrder = _dbContext.PurchaseOrders.Find(purchaseOrderId);
if (existingOrder != null)
{
_dbContext.PurchaseOrders.Remove(existingOrder);
}
}
}
}

_dbContext.SaveChanges();
return Ok(crudRequest);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//using Microsoft.AspNetCore.Mvc;

//namespace PostgreSQL.Server.Controllers
//{
// [ApiController]
// [Route("[controller]")]
// public class WeatherForecastController : ControllerBase
// {
// private static readonly string[] Summaries =
// [
// "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
// ];

// [HttpGet(Name = "GetWeatherForecast")]
// public IEnumerable<WeatherForecast> Get()
// {
// return Enumerable.Range(1, 5).Select(index => new WeatherForecast
// {
// Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
// TemperatureC = Random.Shared.Next(-20, 55),
// Summary = Summaries[Random.Shared.Next(Summaries.Length)]
// })
// .ToArray();
// }
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.ComponentModel.DataAnnotations;

namespace Grid_PostgreSQL.Server.Data
{
/// <summary>
/// Represents a purchase order record mapped to the 'PurchaseOrder' table in the database.
/// This model defines the structure of purchase order-related data used throughout the application.
/// </summary>
public class PurchaseOrder
{
/// <summary>
/// Gets or sets the unique identifier for the purchase order record.
/// This is the primary key and auto-incremented by the database.
/// </summary>
[Key]
public int PurchaseOrderId { get; set; }

/// <summary>
/// Gets or sets the public-facing purchase order number (e.g., PO-2025-0001).
/// This is a unique identifier visible to users and external systems.
/// </summary>
public string? PoNumber { get; set; }

/// <summary>
/// Gets or sets the vendor identifier (e.g., VEN-9001).
/// Links the purchase order to a specific vendor.
/// </summary>
public string? VendorID { get; set; }

/// <summary>
/// Gets or sets the name or description of the item being ordered.
/// </summary>
public string? ItemName { get; set; }

/// <summary>
/// Gets or sets the category of the item (e.g., Electronics, Office Supplies, Hardware).
/// </summary>
public string? ItemCategory { get; set; }

/// <summary>
/// Gets or sets the quantity of items being ordered.
/// Must be a positive integer.
/// </summary>
public int Quantity { get; set; }

/// <summary>
/// Gets or sets the unit price of each item (e.g., 899.99).
/// Stored with precision of 12 digits and 2 decimal places.
/// </summary>
public decimal UnitPrice { get; set; }

/// <summary>
/// Gets or sets the total amount for the order (Quantity × UnitPrice).
/// Auto-calculated and stored with precision of 14 digits and 2 decimal places.
/// </summary>
public decimal? TotalAmount { get; set; }

/// <summary>
/// Gets or sets the current status of the purchase order.
/// Possible values: Pending, Approved, Ordered, Received, Canceled, Completed.
/// </summary>
public string? Status { get; set; }

/// <summary>
/// Gets or sets the name of the person who created the purchase order.
/// </summary>
public string? OrderedBy { get; set; }

/// <summary>
/// Gets or sets the name of the person who approved the purchase order.
/// </summary>
public string? ApprovedBy { get; set; }

/// <summary>
/// Gets or sets the date when the purchase order was placed.
/// </summary>
public DateTime OrderDate { get; set; }

/// <summary>
/// Gets or sets the expected delivery date for the ordered items.
/// </summary>
public DateTime? ExpectedDeliveryDate { get; set; }

/// <summary>
/// Gets or sets the timestamp indicating when the purchase order record was created.
/// Auto-set to the current date and time by the database.
/// </summary>
public DateTimeOffset CreatedOn { get; set; }

/// <summary>
/// Gets or sets the timestamp indicating when the purchase order record was last updated.
/// Auto-updated to the current date and time whenever a modification occurs.
/// </summary>
public DateTimeOffset UpdatedOn { get; set; }
}
}
Loading