diff --git a/Src/Controllers/SellerController.cs b/Src/Controllers/SellerController.cs new file mode 100644 index 000000000..6092dd3ad --- /dev/null +++ b/Src/Controllers/SellerController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Mvc; +using Src.Models; +using Src.Repositories; + +namespace Src.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class SellerController : ControllerBase + { + private readonly SellerRepository _sellerRepository; + + public SellerController(SellerRepository sellerRepository) + { + _sellerRepository = sellerRepository; + } + + [HttpGet("{id}")] + public ActionResult GetSeller(int id) + { + var seller = _sellerRepository.GetSellerById(id); + if (seller == null) + { + return NotFound(); + } + return Ok(seller); + } + + [HttpPost] + public ActionResult CreateSeller(Seller seller) + { + _sellerRepository.AddSeller(seller); + return CreatedAtAction(nameof(GetSeller), new { id = seller.Id }, seller); + } + } +} diff --git a/Src/Models/Seller.cs b/Src/Models/Seller.cs new file mode 100644 index 000000000..147e0a0d7 --- /dev/null +++ b/Src/Models/Seller.cs @@ -0,0 +1,11 @@ +namespace Src.Models +{ + public class Seller + { + public int Id { get; set; } + public string Name { get; set; } + public string ContactInfo { get; set; } + + // Additional properties can be added here + } +} diff --git a/Src/Repositories/SellerRepository.cs b/Src/Repositories/SellerRepository.cs new file mode 100644 index 000000000..7f90b0d25 --- /dev/null +++ b/Src/Repositories/SellerRepository.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using Src.Models; + +namespace Src.Repositories +{ + public class SellerRepository + { + private readonly List _sellers = new List(); + + public Seller GetSellerById(int id) + { + return _sellers.Find(s => s.Id == id); + } + + public void AddSeller(Seller seller) + { + _sellers.Add(seller); + } + + public void UpdateSeller(Seller seller) + { + var existingSeller = GetSellerById(seller.Id); + if (existingSeller != null) + { + existingSeller.Name = seller.Name; + existingSeller.ContactInfo = seller.ContactInfo; + } + } + } +} diff --git a/Tests/SellerAPI.Tests/SellerControllerTests.cs b/Tests/SellerAPI.Tests/SellerControllerTests.cs new file mode 100644 index 000000000..7ab4fca6e --- /dev/null +++ b/Tests/SellerAPI.Tests/SellerControllerTests.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Mvc; +using Src.Controllers; +using Src.Models; +using Src.Repositories; +using Xunit; + +namespace Tests.SellerAPI.Tests +{ + public class SellerControllerTests + { + private readonly SellerController _controller; + private readonly SellerRepository _repository; + + public SellerControllerTests() + { + _repository = new SellerRepository(); + _controller = new SellerController(_repository); + } + + [Fact] + public void GetSeller_ReturnsNotFound_WhenSellerDoesNotExist() + { + var result = _controller.GetSeller(1); + Assert.IsType(result.Result); + } + + [Fact] + public void CreateSeller_ReturnsCreatedAtActionResult() + { + var seller = new Seller + { + Id = 1, + Name = "Test Seller", + ContactInfo = "test@example.com" + }; + var result = _controller.CreateSeller(seller); + Assert.IsType(result); + } + + [Fact] + public void GetSeller_ReturnsSeller_WhenSellerExists() + { + var seller = new Seller { Id = 1, Name = "Test Seller", ContactInfo = "test@example.com" }; + _repository.AddSeller(seller); + var result = _controller.GetSeller(1); + Assert.IsType(result.Result); + } + } +}