Advertisement
otkalce

State persistence - URL

Mar 20th, 2023
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | Source Code | 0 0
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3.  
  4. namespace persistence_examples.Controllers
  5. {
  6.     public class Receipt
  7.     {
  8.         public int Id { get; set; }
  9.         public string Title { get; set; }
  10.         public decimal Total { get; set; }
  11.         public List<ReceiptItem> Items { get; set; }
  12.     }
  13.  
  14.     public class ReceiptItem
  15.     {
  16.         public int ItemNo { get; set; }
  17.         public string ProductName { get; set; }
  18.         public int Quantity { get; set; }
  19.         public decimal Price { get; set; }
  20.         public decimal Value { get; set; }
  21.     }
  22.  
  23.     [Route("api/[controller]")]
  24.     [ApiController]
  25.     public class ReceiptController : ControllerBase
  26.     {
  27.         private static List<Receipt> _receipts = new List<Receipt>();
  28.  
  29.         public ReceiptController()
  30.         {
  31.             // If there are no receipts, generate some for testing
  32.             if (!_receipts.Any())
  33.             {
  34.                 var totals = new decimal[] { 10.99M, 11.99M, 29.99M, 99.99M };
  35.                 var titles = new string[] { "Monthly Subscription", "Monthly Subscription - Family", "Quarterly Subscription", "Annual Subscription" };
  36.                 var currentId = 1;
  37.                 var rnd = new Random();
  38.                 for (var i = 0; i < 100; i++)
  39.                 {
  40.                     var rndIdx = rnd.Next(totals.Length);
  41.                     _receipts.Add(new Receipt
  42.                     {
  43.                         Id = currentId,
  44.                         Total = totals[rndIdx],
  45.                         Title = titles[rndIdx],
  46.                     });
  47.                     currentId++;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         [HttpGet()]
  53.         public ActionResult<Receipt> GetAll()
  54.         {
  55.             return Ok(_receipts);
  56.         }
  57.  
  58.  
  59.         [HttpGet("{id}")]
  60.         public ActionResult<Receipt> Get(int id)
  61.         {
  62.             var retVal = _receipts.FirstOrDefault(x => x.Id == id);
  63.             if(retVal == null)
  64.                 return NotFound();
  65.  
  66.             return Ok(retVal);
  67.         }
  68.  
  69.         [HttpGet("[action]")]
  70.         public ActionResult<Receipt> Paged(int page, int size, string orderBy, string direction)
  71.         {
  72.             IEnumerable<Receipt> ordered;
  73.  
  74.             // Ordering
  75.             if (string.Compare(orderBy, "id", true) == 0)
  76.             {
  77.                 ordered = _receipts.OrderBy(x => x.Id);
  78.             }
  79.             else if (string.Compare(orderBy, "title", true) == 0)
  80.             {
  81.                 ordered = _receipts.OrderBy(x => x.Title);
  82.             }
  83.             else if (string.Compare(orderBy, "total", true) == 0)
  84.             {
  85.                 ordered = _receipts.OrderBy(x => x.Total);
  86.             }
  87.             else
  88.             {
  89.                 // default: order by Id
  90.                 ordered = _receipts.OrderBy(x => x.Id);
  91.             }
  92.  
  93.             // For descending order we just reverse it
  94.             if (string.Compare(direction, "desc", true) == 0)
  95.             {
  96.                 ordered = ordered.Reverse();
  97.             }
  98.  
  99.  
  100.             // Now we can page the correctly ordered items
  101.             var retVal = ordered.Skip(page * size).Take(size);
  102.  
  103.             return Ok(retVal);
  104.         }
  105.  
  106.         [HttpGet("[action]")]
  107.         public ActionResult<Receipt> Filtered(string filter)
  108.         {
  109.             var retVal =
  110.                 _receipts.Where(x =>
  111.                     x.Title.Contains(filter, StringComparison.InvariantCultureIgnoreCase));
  112.  
  113.             return Ok(retVal);
  114.         }
  115.     }
  116. }
  117.  
Tags: state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement