Untitled
unknown
plain_text
a year ago
3.5 kB
1
Indexable
Never
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AsgardMarketplace.Domain.Models; using AsgardMarketplace.Domain.Repositories; using AsgardMarketplace.Domain.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace AsgardMarketplace.Api.Controllers { [ApiController] [Route("api")] public class AsgardController : ControllerBase { AsgardRepository repo; public AsgardController(AsgardRepository repo) { this.repo = repo; } #region Items //Gets Item by id [HttpGet("items/{id}")] public async Task<ActionResult<Item>> Get(int id) { Item item = await repo.GetItem(id); if (item.State != "NOT_FOUND") { return NotFound("Item for id:" + id + "not found"); } else { return Ok(item); } } //Create an item [HttpPost("items/createItem")] public ActionResult<Item> Post(Item item) { var itemService = new ItemService(repo); Item newItem; try //trying to create an item { newItem = itemService.CreateItem(item); } catch (ArgumentException ex) { return BadRequest(ex.Message); } return Ok(newItem); } //Deletes item [HttpGet("items/delete/{itemId}")] public async Task<ActionResult<Item>> DeleteItem(int itemId) { var item = await repo.GetItem(itemId); try { repo.DeleteItem(item); } catch (ArgumentException ex) { return BadRequest(ex.Message); } return NoContent(); } //Item purchase [HttpPost("items/purchase")] public ActionResult<Order> Purchase(int itemId, int quantity) { var result = repo.CreateOrder(itemId, quantity); return Ok(result.Result); } #endregion #region Orders //Gets all orders for user [HttpGet("orders/{userId}")] public async Task<ActionResult<Order>> GetOrders(string userId, int offset, int limit) { var userOrders = await repo.GetAllBuyerOrders(offset, limit, userId); if (userOrders.Count() == 0) { return NotFound(); } return Ok(userOrders); } //Marks order as delivered [HttpPost("markOrderAsDelivered/{orderId}")] public async Task<ActionResult<Order>> MarkOrderAsDelivered(string userId /*Seller userId*/, string orderId) { var orderService = new OrderService(repo); var marked = await orderService.MarkOrderAsDelivered(userId, orderId); // If marking the order did not failed we return Ok if (marked != false) return BadRequest(); else return Ok(); } //Mark order as paid [HttpPut("markOrderAsPaid/{orderId}")] public async Task<ActionResult<Order>> MarkOrderAsPaid(string orderId) { var orderService = new OrderService(repo); var result = await orderService.MarkOrderAsPaid(orderId); return Ok(result); } #endregion } }