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 Brand Subcollection API #343

Closed
45 changes: 45 additions & 0 deletions Src/Controllers/BrandController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Mvc;
using Services;

namespace Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BrandController : ControllerBase
{
private readonly BrandService _brandService;

public BrandController(BrandService brandService)
{
_brandService = brandService;
}

[HttpPost("{brandId}/subcollections/{subcollectionId}")]
public IActionResult AssociateSubcollection(int brandId, int subcollectionId)
{
try
{
_brandService.AssociateSubcollection(brandId, subcollectionId);
return Ok();
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}

[HttpDelete("{brandId}/subcollections/{subcollectionId}")]
public IActionResult DisassociateSubcollection(int brandId, int subcollectionId)
{
try
{
_brandService.DisassociateSubcollection(brandId, subcollectionId);
return Ok();
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
}
}
39 changes: 39 additions & 0 deletions Src/Migrations/AddBrandSubcollectionRelationship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace Migrations
{
public partial class AddBrandSubcollectionRelationship : Migration

Check notice on line 5 in Src/Migrations/AddBrandSubcollectionRelationship.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/Migrations/AddBrandSubcollectionRelationship.cs#L5

'partial' is gratuitous in this context.
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BrandSubcollection",
columns: table => new
{
BrandId = table.Column<int>(nullable: false),
SubcollectionId = table.Column<int>(nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_BrandSubcollection", x => new { x.BrandId, x.SubcollectionId });
table.ForeignKey(
name: "FK_BrandSubcollection_Brands_BrandId",
column: x => x.BrandId,
principalTable: "Brands",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_BrandSubcollection_Subcollections_SubcollectionId",
column: x => x.SubcollectionId,
principalTable: "Subcollections",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: "BrandSubcollection");
}
}
}
5 changes: 5 additions & 0 deletions Src/Models/Brand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Collections.Generic;
/// <summary>
/// Gets or sets the subcollections associated with the brand.
/// </summary>
public ICollection<Subcollection> Subcollections { get; set; }
12 changes: 12 additions & 0 deletions Src/Models/Subcollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace Models
{
public class Subcollection
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Brand> Brands { get; set; }
}
}
50 changes: 50 additions & 0 deletions Src/Services/BrandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Linq;
using Models;

namespace Services
{
public class BrandService
{
private readonly DbContext _context;

public BrandService(DbContext context)
{
_context = context;
}

public void AssociateSubcollection(int brandId, int subcollectionId)
{
var brand = _context.Brands.Find(brandId);
var subcollection = _context.Subcollections.Find(subcollectionId);

if (brand == null || subcollection == null)

Check failure on line 21 in Src/Services/BrandService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/Services/BrandService.cs#L21

Add curly braces around the nested statement(s) in this 'if' block.
throw new ArgumentException("Invalid brand or subcollection ID.");

brand.Subcollections.Add(subcollection);
_context.SaveChanges();
}

public void DisassociateSubcollection(int brandId, int subcollectionId)
{
var brand = _context.Brands.Find(brandId);
var subcollection = _context.Subcollections.Find(subcollectionId);

if (brand == null || subcollection == null)
throw new ArgumentException("Invalid brand or subcollection ID.");

brand.Subcollections.Remove(subcollection);
_context.SaveChanges();
}

public bool BrandExists(int brandId)
{
return _context.Brands.Any(b => b.Id == brandId);
}

public bool SubcollectionExists(int subcollectionId)
{
return _context.Subcollections.Any(s => s.Id == subcollectionId);
}
}
}
Loading