Upload project.
This commit is contained in:
105
ThAmCo-Stock/ThAmCo.Stock/Controllers/AccountController.cs
Normal file
105
ThAmCo-Stock/ThAmCo.Stock/Controllers/AccountController.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using IdentityModel.Client;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ThAmCo.Stock.Models;
|
||||
|
||||
namespace ThAmCo.Stock.Controllers
|
||||
{
|
||||
public class AccountController : Controller
|
||||
{
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
|
||||
public AccountController(IHttpClientFactory clientFactory)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Login()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Login([FromForm] LoginModel model)
|
||||
{
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
|
||||
var disco = await client.GetDiscoveryDocumentAsync("https://localhost:43389");
|
||||
var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
|
||||
{
|
||||
Address = disco.TokenEndpoint,
|
||||
ClientId = "my_web_app",
|
||||
ClientSecret = "secret",
|
||||
|
||||
UserName = model.Email,
|
||||
Password = model.Password
|
||||
});
|
||||
|
||||
if (tokenResponse.IsError)
|
||||
return View();
|
||||
|
||||
var userInfoResponse = await client.GetUserInfoAsync(new UserInfoRequest
|
||||
{
|
||||
Address = disco.UserInfoEndpoint,
|
||||
Token = tokenResponse.AccessToken
|
||||
});
|
||||
|
||||
if (userInfoResponse.IsError)
|
||||
return View();
|
||||
|
||||
var claimsIdentity = new ClaimsIdentity(userInfoResponse.Claims, "Cookies");
|
||||
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
|
||||
|
||||
var tokensToStore = new AuthenticationToken[]
|
||||
{
|
||||
new AuthenticationToken { Name = "access_token", Value = tokenResponse.AccessToken }
|
||||
};
|
||||
var authProperties = new AuthenticationProperties();
|
||||
authProperties.StoreTokens(tokensToStore);
|
||||
|
||||
await HttpContext.SignInAsync("Cookies", claimsPrincipal, authProperties);
|
||||
|
||||
return LocalRedirect("/");
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await HttpContext.SignOutAsync("Cookies");
|
||||
return LocalRedirect("/");
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public IActionResult Authed()
|
||||
{
|
||||
return Ok("Authed");
|
||||
}
|
||||
|
||||
[Authorize(Policy = "StaffOnly")]
|
||||
public IActionResult StaffAuthed()
|
||||
{
|
||||
return Ok("Authed");
|
||||
}
|
||||
|
||||
public IActionResult AccessDenied()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
private HttpClient GetHttpClient(string s)
|
||||
{
|
||||
return _clientFactory.CreateClient(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
ThAmCo-Stock/ThAmCo.Stock/Controllers/HomeController.cs
Normal file
31
ThAmCo-Stock/ThAmCo.Stock/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ThAmCo.Stock.Models;
|
||||
|
||||
namespace ThAmCo.Stock.Controllers
|
||||
{
|
||||
[Authorize(Policy = "StaffOnly")]
|
||||
public class HomeController : Controller
|
||||
{
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
}
|
||||
}
|
||||
169
ThAmCo-Stock/ThAmCo.Stock/Controllers/OldStockController.cs
Normal file
169
ThAmCo-Stock/ThAmCo.Stock/Controllers/OldStockController.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ThAmCo.Stock.Data;
|
||||
using ThAmCo.Stock.Data.StockContext;
|
||||
using ThAmCo.Stock.Models.Dto;
|
||||
using ThAmCo.Stock.Models.ViewModel;
|
||||
|
||||
namespace ThAmCo.Stock.Controllers
|
||||
{
|
||||
[Route("[controller]")]
|
||||
[Controller]
|
||||
public class OldStockController : Controller
|
||||
{
|
||||
private readonly IStockContext _context;
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
|
||||
public OldStockController(IStockContext context, IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_context = context;
|
||||
_clientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
// GET: api/Stock
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<ProductStockDto>>> GetProductStock()
|
||||
{
|
||||
return Ok(await _context.GetAll());
|
||||
}
|
||||
|
||||
// GET: api/Stock/5
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<ProductStockDetailsDto>> Details(int id)
|
||||
{
|
||||
if (id < 0)
|
||||
return NotFound();
|
||||
|
||||
var productStock = await _context.GetProductStockAsync(id);
|
||||
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
|
||||
return Ok(new ProductStockDetailsDto
|
||||
{
|
||||
ProductID = productStock.ProductStock.ProductId,
|
||||
Stock = productStock.ProductStock.Stock,
|
||||
Price = productStock.Price.ProductPrice
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet("PriceHistory/{id}")]
|
||||
public async Task<ActionResult<ProductStockPricingHistoryDto>> PriceHistory(int id)
|
||||
{
|
||||
if (id < 0)
|
||||
return NotFound();
|
||||
|
||||
var productStock = await _context.GetProductStockAsync(id);
|
||||
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
|
||||
var productPrices = _context.GetAllPrices().Result.Where(p => p.ProductStockId == productStock.ProductStock.PriceId).ToList();
|
||||
|
||||
var productDetails = new ProductStockPricingHistoryDto
|
||||
{
|
||||
ProductID = productStock.ProductStock.ProductId,
|
||||
Stock = productStock.ProductStock.Stock,
|
||||
Prices = productPrices
|
||||
};
|
||||
|
||||
return Ok(productDetails);
|
||||
}
|
||||
|
||||
// PUT: api/Stock/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutProductStock(int id, ProductStock productStock)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
/*if (id != productStock.Id)
|
||||
return BadRequest();
|
||||
|
||||
_context.Entry(productStock).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ProductStockExists(id))
|
||||
return NotFound();
|
||||
throw;
|
||||
}
|
||||
|
||||
return NoContent();*/
|
||||
}
|
||||
|
||||
// POST: api/Stock
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ProductStock>> PostProductStock(ProductStock productStock)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
/*_context.ProductStocks.Add(productStock);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction("GetProductStock", new { id = productStock.Id }, productStock);*/
|
||||
}
|
||||
|
||||
// DELETE: api/Stock/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult<ProductStock>> DeleteProductStock(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
/*var productStock = await _context.ProductStocks.FindAsync(id);
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
|
||||
_context.ProductStocks.Remove(productStock);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return productStock;*/
|
||||
}
|
||||
|
||||
[HttpGet("low/{count}")]
|
||||
public ActionResult<IEnumerable<ProductStockDto>> Low(int? count)
|
||||
{
|
||||
if (count != null && count < 0)
|
||||
return NotFound(); //0 is technically valid, since the request would want 0. below 0 would be an invalid request.
|
||||
|
||||
var listToCount = _context.GetAll().Result.OrderBy(ps => ps.ProductStock.Stock).Take(count ?? 5).ToList();
|
||||
|
||||
return View(listToCount);
|
||||
}
|
||||
|
||||
[HttpGet("AdjustCost/{id}")]
|
||||
public async Task<ActionResult<ProductStockDto>> AdjustCost(int id)
|
||||
{
|
||||
var productPrice = await _context.GetProductStockAsync(id);
|
||||
if (productPrice == null)
|
||||
return NotFound();
|
||||
return View(new AdjustCostViewModel { Id = productPrice.ProductStock.Id, Cost = productPrice.Price.ProductPrice });
|
||||
}
|
||||
|
||||
[HttpPost("AdjustCost")]
|
||||
public async Task<ActionResult> AdjustCost(int id, double cost)
|
||||
{
|
||||
var productStock = _context.GetProductStockAsync(id).Result;
|
||||
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
|
||||
var price = _context.AddPriceAsync(new Price { ProductPrice = cost, ProductStockId = id, Date = DateTime.Now });
|
||||
|
||||
productStock.ProductStock.PriceId = price.Id;
|
||||
_context.UpdateProductStockAsync(productStock.ProductStock);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
private bool ProductStockExists(int id)
|
||||
{
|
||||
return _context.GetAll().Result.Any(e => e.ProductStock.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
440
ThAmCo-Stock/ThAmCo.Stock/Controllers/StockController.cs
Normal file
440
ThAmCo-Stock/ThAmCo.Stock/Controllers/StockController.cs
Normal file
@@ -0,0 +1,440 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using ThAmCo.Stock.Data;
|
||||
using ThAmCo.Stock.Data.StockContext;
|
||||
using ThAmCo.Stock.Models.Dto;
|
||||
using ThAmCo.Stock.Models.ViewModel;
|
||||
|
||||
namespace ThAmCo.Stock.Controllers
|
||||
{
|
||||
[Authorize(Policy = "StaffOnly")]
|
||||
public class StockController : Controller
|
||||
{
|
||||
private readonly IStockContext _context;
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
|
||||
public HttpClient HttpClient { get; set; }
|
||||
|
||||
public StockController(IStockContext context, IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_context = context;
|
||||
_clientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
// GET: Stock
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return View(_context.GetAll().Result.ToList());
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
// GET: Stock/Details/5
|
||||
public async Task<ActionResult<ProductStockDetailsDto>> Details(int id)
|
||||
{
|
||||
if (id < 0)
|
||||
return NotFound();
|
||||
|
||||
var productStock = await _context.GetProductStockAsync(id);
|
||||
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
|
||||
return Ok(new ProductStockDetailsDto
|
||||
{
|
||||
ProductID = productStock.ProductStock.ProductId,
|
||||
Stock = productStock.ProductStock.Stock,
|
||||
Price = productStock.Price.ProductPrice
|
||||
});
|
||||
}
|
||||
|
||||
// GET: Stock/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Stock/Create
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create([Bind("Id,ProductId,Stock,PriceId")] ProductStock productStock)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
//_context.AddProductStockAsync(productStock);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(productStock);
|
||||
}
|
||||
|
||||
// GET: Stock/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var productStock = await _context.GetProductStockAsync(id ?? 1);
|
||||
if (productStock == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return View(productStock);
|
||||
}
|
||||
|
||||
// POST: Stock/Edit/5
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(int id, [Bind("Id,ProductId,Stock,PriceId")] ProductStock productStock)
|
||||
{
|
||||
if (id != productStock.Id)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.UpdateProductStockAsync(productStock);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ProductStockExists(productStock.Id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(productStock);
|
||||
}
|
||||
|
||||
// GET: Stock/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var productStock = await _context.GetProductStockAsync(id ?? 1);
|
||||
if (productStock == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(productStock);
|
||||
}
|
||||
|
||||
// POST: Stock/Delete/5
|
||||
[HttpPost, ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
/*var productStock = await _context.ProductStocks.FindAsync(id);
|
||||
_context.ProductStocks.Remove(productStock);
|
||||
await _context.SaveChangesAsync();*/
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<IEnumerable<ProductStockDto>>> ProductStocks()
|
||||
{
|
||||
return Ok(await _context.GetAll());
|
||||
}
|
||||
|
||||
public async Task<ActionResult<ProductStockPricingHistoryDto>> PriceHistory(int id)
|
||||
{
|
||||
if (id < 0)
|
||||
return NotFound();
|
||||
|
||||
var productStock = await _context.GetProductStockAsync(id);
|
||||
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
|
||||
var productPrices = _context.GetAllPrices().Result.Where(p => p.ProductStockId == productStock.ProductStock.PriceId).ToList();
|
||||
|
||||
var productDetails = new ProductStockPricingHistoryDto
|
||||
{
|
||||
ProductID = productStock.ProductStock.ProductId,
|
||||
Stock = productStock.ProductStock.Stock,
|
||||
Prices = productPrices
|
||||
};
|
||||
|
||||
return Ok(productDetails);
|
||||
}
|
||||
|
||||
public ActionResult<IEnumerable<ProductStockDto>> Low(int? count)
|
||||
{
|
||||
if (count != null && count < 0)
|
||||
return NotFound(); //0 is technically valid, since the request would want 0. below 0 would be an invalid request.
|
||||
|
||||
var listToCount = _context.GetAll().Result.OrderBy(ps => ps.ProductStock.Stock).Take(count ?? 5).ToList();
|
||||
|
||||
return View(listToCount);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<AdjustCostViewModel>> AdjustCost(int id)
|
||||
{
|
||||
var productPrice = await _context.GetProductStockAsync(id);
|
||||
|
||||
if (productPrice == null)
|
||||
return NotFound();
|
||||
|
||||
AdjustCostViewModel objectResponse;
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
|
||||
|
||||
var response = await client.GetAsync("https://localhost:44375/products/getProduct/" + id);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var objectResult = await response.Content.ReadAsAsync<ProductDto>();
|
||||
if (objectResult == null)
|
||||
return NotFound();
|
||||
objectResponse = new AdjustCostViewModel
|
||||
{
|
||||
Id = productPrice.ProductStock.Id,
|
||||
Cost = productPrice.Price.ProductPrice,
|
||||
Name = objectResult.Name,
|
||||
Description = objectResult.Description
|
||||
};
|
||||
}
|
||||
else return NotFound();
|
||||
|
||||
return View(objectResponse);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> AdjustCost(int id, double cost)
|
||||
{
|
||||
var productStock = await _context.GetProductStockAsync(id);
|
||||
|
||||
if (productStock == null)
|
||||
return NotFound();
|
||||
if (cost <= 0)
|
||||
return BadRequest();
|
||||
|
||||
var price = _context.AddPriceAsync(new Price { ProductPrice = cost, ProductStockId = id, Date = DateTime.Now });
|
||||
|
||||
productStock.ProductStock.PriceId = price.Id;
|
||||
_context.UpdateProductStockAsync(productStock.ProductStock);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> VendorProducts(string supplier)
|
||||
{
|
||||
var vendorProducts = new VendorProductIndexModel
|
||||
{
|
||||
Vendor = supplier
|
||||
};
|
||||
|
||||
HttpResponseMessage response = null;
|
||||
|
||||
var url = GetURLForSupplier(supplier);
|
||||
if (url == null)
|
||||
return NotFound();
|
||||
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
|
||||
|
||||
response = await client.GetAsync(url + "product");
|
||||
|
||||
if (response?.IsSuccessStatusCode == true)
|
||||
{
|
||||
var objectResult = await response.Content.ReadAsAsync<List<VendorProductDto>>();
|
||||
if (objectResult == null)
|
||||
goto View;
|
||||
vendorProducts.Products = objectResult;
|
||||
}
|
||||
else return NotFound();
|
||||
|
||||
View:
|
||||
return View(vendorProducts);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> OrderRequest(int id, string supplier = null)
|
||||
{
|
||||
if (id <= 0)
|
||||
return NotFound();
|
||||
|
||||
var url = GetURLForSupplier(supplier);
|
||||
if (url == null)
|
||||
return NotFound();
|
||||
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
|
||||
|
||||
HttpResponseMessage response = null;
|
||||
response = await client.GetAsync(url + "product/" + id);
|
||||
|
||||
if (response?.IsSuccessStatusCode == true)
|
||||
{
|
||||
var objectResult = await response.Content.ReadAsAsync<VendorProductDto>();
|
||||
|
||||
if (objectResult == null)
|
||||
return NotFound();
|
||||
if (objectResult.Id != id)
|
||||
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
||||
|
||||
return View(new OrderRequestModel { Id = id, Name = objectResult.Name, Description = objectResult.Description, Supplier = supplier });
|
||||
}
|
||||
else return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> OrderRequestSubmitted(int id, string supplier, int quantity)
|
||||
{
|
||||
var orderRequest = new OrderRequest
|
||||
{
|
||||
Quantity = quantity,
|
||||
Supplier = supplier,
|
||||
SubmittedTime = DateTime.Now,
|
||||
Approved = false,
|
||||
ApprovedTime = null,
|
||||
Deleted = false
|
||||
};
|
||||
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
|
||||
|
||||
var url = GetURLForSupplier(supplier);
|
||||
if (url == null)
|
||||
return NotFound();
|
||||
|
||||
HttpResponseMessage response = null;
|
||||
|
||||
response = await client.GetAsync(url + "product/" + id);
|
||||
|
||||
if (response?.IsSuccessStatusCode == true)
|
||||
{
|
||||
var objectResult = await response.Content.ReadAsAsync<VendorProductDto>();
|
||||
if (objectResult == null)
|
||||
return NotFound();
|
||||
orderRequest.ProductId = objectResult.Id;
|
||||
orderRequest.Price = objectResult.Price * quantity;
|
||||
|
||||
_context.AddOrderRequest(orderRequest);
|
||||
|
||||
return RedirectToAction(nameof(VendorProducts), new { supplier });
|
||||
}
|
||||
else return NotFound();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> OrderRequests()
|
||||
{
|
||||
return View(_context.GetAllOrderRequests().Result.Where(or => !or.Approved).ToList());
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> OrderRequestReview(int id)
|
||||
{
|
||||
var orderRequest = _context.GetOrderRequest(id).Result;
|
||||
if (orderRequest == null)
|
||||
return NotFound();
|
||||
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
|
||||
|
||||
var url = GetURLForSupplier(orderRequest.Supplier);
|
||||
if (url == null)
|
||||
return NotFound();
|
||||
|
||||
HttpResponseMessage response = null;
|
||||
|
||||
response = await client.GetAsync(url + "product/" + orderRequest.ProductId);
|
||||
|
||||
if (response?.IsSuccessStatusCode == true)
|
||||
{
|
||||
var objectResult = await response.Content.ReadAsAsync<VendorProductDto>();
|
||||
if (objectResult == null)
|
||||
return NotFound();
|
||||
|
||||
//Update order details incase service changes prices.
|
||||
if (orderRequest.Price != objectResult.Price * orderRequest.Quantity)
|
||||
{
|
||||
orderRequest.Price = objectResult.Price * orderRequest.Quantity;
|
||||
_context.UpdateOrderRequest(orderRequest);
|
||||
}
|
||||
|
||||
var reviewModel = new OrderRequestReviewModel
|
||||
{
|
||||
Id = orderRequest.Id,
|
||||
Name = objectResult.Name,
|
||||
Description = objectResult.Description,
|
||||
Price = orderRequest.Price,
|
||||
Quantity = orderRequest.Quantity,
|
||||
Supplier = orderRequest.Supplier
|
||||
};
|
||||
|
||||
return View(reviewModel);
|
||||
}
|
||||
else return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> OrderRequestApproved(int id)
|
||||
{
|
||||
var orderRequest = _context.GetOrderRequest(id).Result;
|
||||
if (orderRequest == null)
|
||||
return NotFound();
|
||||
var orderDto = new ProductOrderPostDto
|
||||
{
|
||||
AccountName = "sample string 1",
|
||||
CardNumber = "sample string 2",
|
||||
ProductId = orderRequest.ProductId,
|
||||
Quantity = orderRequest.Quantity
|
||||
};
|
||||
|
||||
var client = GetHttpClient("StandardRequest");
|
||||
var result = await client.PostAsJsonAsync(GetURLForSupplier(orderRequest.Supplier) + "order", orderDto);
|
||||
if (result.IsSuccessStatusCode)
|
||||
{
|
||||
_context.ApproveOrderRequest(id);
|
||||
return Ok(result.Content);
|
||||
//return RedirectToAction(nameof(OrderRequests));
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
private bool ProductStockExists(int id)
|
||||
{
|
||||
return _context.GetAll().Result.Any(e => e.ProductStock.Id == id);
|
||||
}
|
||||
|
||||
private HttpClient GetHttpClient(string s)
|
||||
{
|
||||
if (_clientFactory == null && HttpClient != null) return HttpClient;
|
||||
|
||||
return _clientFactory.CreateClient(s);
|
||||
}
|
||||
|
||||
private string GetURLForSupplier(string s)
|
||||
{
|
||||
if (s == "undercutters") return "http://undercutters.azurewebsites.net/api/";
|
||||
else if (s == "dodgydealers") return "http://dodgydealers.azurewebsites.net/api/";
|
||||
else return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user