Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitAuto: [FEATURE] Implement Seller API #351

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
36 changes: 36 additions & 0 deletions Src/Controllers/SellerController.cs
Original file line number Diff line number Diff line change
@@ -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<Seller> 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);
}
}
}
11 changes: 11 additions & 0 deletions Src/Models/Seller.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
30 changes: 30 additions & 0 deletions Src/Repositories/SellerRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using Src.Models;

namespace Src.Repositories
{
public class SellerRepository
{
private readonly List<Seller> _sellers = new List<Seller>();

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;
}
}
}
}
44 changes: 44 additions & 0 deletions Tests/SellerAPI.Tests/SellerControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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<NotFoundResult>(result.Result);
}

[Fact]
public void CreateSeller_ReturnsCreatedAtActionResult()
{
var seller = new Seller { Id = 1, Name = "Test Seller", ContactInfo = "[email protected]" };
var result = _controller.CreateSeller(seller);
Assert.IsType<CreatedAtActionResult>(result);
}

[Fact]
public void GetSeller_ReturnsSeller_WhenSellerExists()
{
var seller = new Seller { Id = 1, Name = "Test Seller", ContactInfo = "[email protected]" };
_repository.AddSeller(seller);
var result = _controller.GetSeller(1);
Assert.IsType<OkObjectResult>(result.Result);
}
}
}
Loading