Skip to content

Commit

Permalink
Added CA and Stylecop rules to NorthwindCRUD project.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudioig committed Oct 20, 2023
1 parent aeec22a commit ffc7c06
Show file tree
Hide file tree
Showing 69 changed files with 670 additions and 563 deletions.
5 changes: 3 additions & 2 deletions NorthwindCRUD.Tests/CategroyServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public void ShouldCreateCategory()
Assert.IsNotNull(createdCategory);
createdCategory = DataHelper2.CategoryService.GetById(createdCategory.CategoryId);

Assert.AreEqual(category.Name, createdCategory.Name);
Assert.AreEqual(category.Description, createdCategory.Description);
Assert.AreEqual(category.Name, createdCategory?.Name);
Assert.AreEqual(category.Description, createdCategory?.Description);
}

[TestMethod]
Expand All @@ -32,6 +32,7 @@ public void ShouldUpdateCategory()

Assert.IsNotNull(updatedCategory);
updatedCategory = DataHelper2.CategoryService.GetById(updatedCategory.CategoryId);
Assert.IsNotNull(updatedCategory);
Assert.AreNotEqual(orignalName, updatedCategory.Name);
Assert.AreNotEqual(orignalDescription, updatedCategory.Description);
Assert.AreEqual(createdCategory.Name, updatedCategory.Name);
Expand Down
1 change: 1 addition & 0 deletions NorthwindCRUD.Tests/CustomerServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void ShouldCreateCustomer()
Assert.AreEqual(customer, createdCustomer, "Customers instances should be the same since we are over the same context");

createdCustomer = DataHelper2.CustomerService.GetById(createdCustomer.CustomerId);
Assert.IsNotNull(createdCustomer);
Assert.AreNotEqual(customer, createdCustomer, "Customer instances should be different");

Assert.AreEqual(customer.CompanyName, createdCustomer.CompanyName);
Expand Down
11 changes: 6 additions & 5 deletions NorthwindCRUD.Tests/EmployeeServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public void ShouldCreateEmployee()
var createdEmployee = DataHelper.EmployeeService.Create(employee);

Assert.IsNotNull(createdEmployee);
var newEmployee = DataHelper2.EmployeeService.GetById(createdEmployee.EmployeeId);
Assert.AreEqual(employee.FirstName, newEmployee.FirstName);
Assert.AreEqual(employee.LastName, newEmployee.LastName);
Assert.AreEqual(employee.Title, newEmployee.Title);
createdEmployee = DataHelper2.EmployeeService.GetById(createdEmployee.EmployeeId);
Assert.IsNotNull(createdEmployee);
Assert.AreEqual(employee.FirstName, createdEmployee.FirstName);
Assert.AreEqual(employee.LastName, createdEmployee.LastName);
Assert.AreEqual(employee.Title, createdEmployee.Title);
}

[TestMethod]
Expand All @@ -32,7 +33,7 @@ public void ShouldUpdateEmployee()

Assert.IsNotNull(updatedEmployee);
updatedEmployee = DataHelper2.EmployeeService.GetById(updatedEmployee.EmployeeId);

Assert.IsNotNull(updatedEmployee);
Assert.AreNotEqual(originalTitle, updatedEmployee.Title);
Assert.AreEqual(createdEmployee.Title, updatedEmployee.Title);
}
Expand Down
3 changes: 3 additions & 0 deletions NorthwindCRUD.Tests/OrderServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public void ShouldCreateOrder()

Assert.IsNotNull(createdOrder);
createdOrder = DataHelper2.OrderService.GetById(createdOrder.OrderId);
Assert.IsNotNull(createdOrder);

Assert.AreEqual(order.OrderId, createdOrder.OrderId);
Assert.AreEqual(order.CustomerId, createdOrder.CustomerId);
Assert.AreEqual(order.EmployeeId, createdOrder.EmployeeId);
Expand All @@ -39,6 +41,7 @@ public void ShouldUpdateOrder()

Assert.IsNotNull(updatedOrder);
updatedOrder = DataHelper2.OrderService.GetById(updatedOrder.OrderId);
Assert.IsNotNull(updatedOrder);
Assert.AreNotEqual(originalCustomerId, updatedOrder.CustomerId);
Assert.AreNotEqual(originalEmployeeId, updatedOrder.EmployeeId);
Assert.AreEqual(order.CustomerId, updatedOrder.CustomerId);
Expand Down
4 changes: 2 additions & 2 deletions NorthwindCRUD.Tests/ProductServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void ShouldCreateProduct()

Assert.IsNotNull(createdProduct);
createdProduct = DataHelper2.ProductService.GetById(createdProduct.ProductId);

Assert.IsNotNull(createdProduct);
Assert.AreEqual(product.UnitPrice, createdProduct.UnitPrice);
Assert.AreEqual(product.UnitsInStock, createdProduct.UnitsInStock);
}
Expand All @@ -31,7 +31,7 @@ public void ShouldUpdateProduct()

Assert.IsNotNull(updatedProduct);
updatedProduct = DataHelper2.ProductService.GetById(updatedProduct.ProductId);

Assert.IsNotNull(updatedProduct);
Assert.AreNotEqual(originaUnitPrice, updatedProduct.UnitPrice);
Assert.AreNotEqual(originaUnitsInStock, updatedProduct.UnitsInStock);

Expand Down
2 changes: 2 additions & 0 deletions NorthwindCRUD.Tests/RegionServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void ShouldCreateRegion()

Assert.IsNotNull(createdRegion);
createdRegion = DataHelper2.RegionService.GetById(createdRegion.RegionId);
Assert.IsNotNull(createdRegion);
Assert.AreEqual(region.RegionId, createdRegion.RegionId);
Assert.AreEqual(region.RegionDescription, createdRegion.RegionDescription);
}
Expand All @@ -30,6 +31,7 @@ public void ShouldUpdateRegion()

Assert.IsNotNull(updatedRegion);
updatedRegion = DataHelper2.RegionService.GetById(updatedRegion.RegionId);
Assert.IsNotNull(updatedRegion);
Assert.AreNotEqual(originalRegionDescription, updatedRegion.RegionDescription);
Assert.AreEqual(region.RegionDescription, updatedRegion.RegionDescription);
}
Expand Down
2 changes: 2 additions & 0 deletions NorthwindCRUD.Tests/ShipperServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void ShouldCreateShipper()

Assert.IsNotNull(createdShipper);
createdShipper = DataHelper2.ShipperService.GetById(createdShipper.ShipperId);
Assert.IsNotNull(createdShipper);
Assert.AreEqual(shipper.ShipperId, createdShipper.ShipperId);
Assert.AreEqual(shipper.CompanyName, createdShipper.CompanyName);
Assert.AreEqual(shipper.Phone, createdShipper.Phone);
Expand All @@ -34,6 +35,7 @@ public void ShouldUpdateShipper()

Assert.IsNotNull(updatedShipper);
updatedShipper = DataHelper2.ShipperService.GetById(updatedShipper.ShipperId);
Assert.IsNotNull(updatedShipper);
Assert.AreNotEqual(originalCompanyName, updatedShipper.CompanyName);
Assert.AreNotEqual(originalPhone, updatedShipper.Phone);
Assert.AreEqual(shipper.CompanyName, updatedShipper.CompanyName);
Expand Down
4 changes: 2 additions & 2 deletions NorthwindCRUD.Tests/SupplierServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ShouldCreateSupplier()

Assert.IsNotNull(createdSupplier);
createdSupplier = DataHelper2.SupplierService.GetById(createdSupplier.SupplierId);

Assert.IsNotNull(createdSupplier);
Assert.AreEqual(supplier.SupplierId, createdSupplier.SupplierId);
Assert.AreEqual(supplier.CompanyName, createdSupplier.CompanyName);
Assert.AreEqual(supplier.ContactName, createdSupplier.ContactName);
Expand All @@ -35,9 +35,9 @@ public void ShouldUpdateSupplier()
supplier.ContactTitle = "Updated Title";

var updatedSupplier = DataHelper.SupplierService.Update(supplier);

Assert.IsNotNull(updatedSupplier);
updatedSupplier = DataHelper2.SupplierService.GetById(updatedSupplier.SupplierId);
Assert.IsNotNull(updatedSupplier);
Assert.AreEqual(supplier.CompanyName, updatedSupplier.CompanyName);
Assert.AreEqual(supplier.ContactName, updatedSupplier.ContactName);
Assert.AreEqual(supplier.ContactTitle, updatedSupplier.ContactTitle);
Expand Down
4 changes: 2 additions & 2 deletions NorthwindCRUD.Tests/TerritoryServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void ShouldCreateTerritory()

Assert.IsNotNull(createdTerritory);
createdTerritory = DataHelper2.TerritoryService.GetById(createdTerritory.TerritoryId);

Assert.IsNotNull(createdTerritory);
Assert.AreEqual(territory.TerritoryId, createdTerritory.TerritoryId);
Assert.AreEqual(territory.TerritoryDescription, createdTerritory.TerritoryDescription);
Assert.AreEqual(territory.RegionId, createdTerritory.RegionId);
Expand All @@ -32,8 +32,8 @@ public void ShouldUpdateTerritory()
var updatedTerritory = DataHelper.TerritoryService.Update(createdTerritory);

Assert.IsNotNull(updatedTerritory);

updatedTerritory = DataHelper2.TerritoryService.GetById(updatedTerritory.TerritoryId);
Assert.IsNotNull(updatedTerritory);
Assert.AreNotEqual(originalTerritoryDescription, updatedTerritory.TerritoryDescription);
Assert.AreEqual(createdTerritory.TerritoryDescription, updatedTerritory.TerritoryDescription);
}
Expand Down
4 changes: 2 additions & 2 deletions NorthwindCRUD/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AuthController : Controller
private readonly AuthService authService;
private readonly IMapper mapper;
private readonly ILogger logger;

public AuthController(IConfiguration configuration, AuthService authService, IMapper mapper, ILogger logger)
{
this.configuration = configuration;
Expand All @@ -38,6 +38,7 @@ public ActionResult<string> Login(LoginDto userModel)

return Ok(token);
}

return BadRequest("Email or password are not correct!");
}

Expand Down Expand Up @@ -75,7 +76,6 @@ public ActionResult<string> Register(RegisterDto userModel)
{
var token = this.authService.GenerateJwtToken(user.Email);
return Ok(token);

}

return BadRequest("Email or password are not correct!");
Expand Down
12 changes: 5 additions & 7 deletions NorthwindCRUD/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CategoriesController(CategoryService categoryService, ProductService prod
this.categoryService = categoryService;
this.productService = productService;
this.mapper = mapper;
this.logger = logger;
this.logger = logger;
}

[HttpGet]
Expand All @@ -30,14 +30,13 @@ public ActionResult<CategoryDto[]> GetAll()
try
{
var categories = this.categoryService.GetAll();
return base.Ok(this.mapper.Map<CategoryDb[], CategoryDto[]>(categories));
return Ok(this.mapper.Map<CategoryDb[], CategoryDto[]>(categories));
}
catch (Exception error)
{
logger.LogError(error.Message);
return StatusCode(500);
}

}

[HttpGet("{id}")]
Expand All @@ -48,7 +47,7 @@ public ActionResult<CategoryDto> GetById(int id)
var category = this.categoryService.GetById(id);
if (category != null)
{
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
return Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
}

return NotFound();
Expand All @@ -60,7 +59,6 @@ public ActionResult<CategoryDto> GetById(int id)
}
}


[HttpGet("{id}/Details")]
public ActionResult<CategoryDetailsDto> GetDetailsById(int id)
{
Expand Down Expand Up @@ -131,7 +129,7 @@ public ActionResult<CategoryDto> Update(CategoryDto model)

if (category != null)
{
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
return Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
}

return NotFound();
Expand All @@ -155,7 +153,7 @@ public ActionResult<CategoryDto> Delete(int id)
var category = this.categoryService.Delete(id);
if (category != null)
{
return base.Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
return Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
}

return NotFound();
Expand Down
6 changes: 6 additions & 0 deletions NorthwindCRUD/Controllers/EmployeesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public ActionResult<EmployeeDto[]> GetTeritoriesByEmployeeId(int id)
try
{
var teritories = this.employeeTerritoryService.GetTeritoriesByEmployeeId(id);
if (teritories == null)
{
return NotFound($"No territories for employee {id}");
}

return Ok(this.mapper.Map<TerritoryDb[], TerritoryDto[]>(teritories));
}
catch (Exception error)
Expand All @@ -144,6 +149,7 @@ public ActionResult<EmployeeTerritoryDto> AddTerritoryToEmployee(EmployeeTerrito
var employeeTerrirtory = this.employeeTerritoryService.AddTerritoryToEmployee(mappedModel);
return Ok(this.mapper.Map<EmployeeTerritoryDb, EmployeeTerritoryDto>(employeeTerrirtory));
}

return BadRequest(ModelState);
}
catch (InvalidOperationException exception)
Expand Down
3 changes: 1 addition & 2 deletions NorthwindCRUD/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ActionResult<CustomerDto> GetCustomerByOrderId(int id)
try
{
var order = this.orderService.GetById(id);
if (order != null)
if (order != null && order.CustomerId != null)
{
var customer = this.customerService.GetById(order.CustomerId);

Expand Down Expand Up @@ -161,7 +161,6 @@ public ActionResult<CustomerDto> GetShipperByOrderId(int id)
}
}


[HttpGet("{id}/Products")]
public ActionResult<ProductDto[]> GetProductsByOrderId(int id)
{
Expand Down
5 changes: 2 additions & 3 deletions NorthwindCRUD/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ProductsController(ProductService productService, CategoryService categor
this.orderService = orderService;
this.supplierService = supplierService;
this.mapper = mapper;
this.logger = logger;
this.logger = logger;
}

[HttpGet]
Expand All @@ -41,7 +41,6 @@ public ActionResult<ProductDto[]> GetAll()
logger.LogError(error.Message);
return StatusCode(500);
}

}

[HttpGet("{id}")]
Expand Down Expand Up @@ -76,7 +75,7 @@ public ActionResult<CategoryDto> GetCategoryByProductId(int id)

if (category != null)
{
return Ok(this.mapper.Map<CategoryDb , CategoryDto>(category));
return Ok(this.mapper.Map<CategoryDb, CategoryDto>(category));
}
}

Expand Down
5 changes: 2 additions & 3 deletions NorthwindCRUD/Controllers/RegionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public ActionResult<RegionDto[]> GetAll()
logger.LogError(error.Message);
return StatusCode(500);
}

}

[HttpGet("{id}")]
Expand Down Expand Up @@ -90,8 +89,8 @@ public ActionResult<RegionDto> Create(RegionDto model)
if (ModelState.IsValid)
{
var mappedModel = this.mapper.Map<RegionDto, RegionDb>(model);
var Region = this.regionService.Create(mappedModel);
return Ok(this.mapper.Map<RegionDb, RegionDto>(Region));
var region = this.regionService.Create(mappedModel);
return Ok(this.mapper.Map<RegionDb, RegionDto>(region));
}

return BadRequest(ModelState);
Expand Down
21 changes: 10 additions & 11 deletions NorthwindCRUD/Controllers/SalesController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace NorthwindCRUD.Controllers
{
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NorthwindCRUD.Models.Dtos;
using NorthwindCRUD.Services;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NorthwindCRUD.Models.Dtos;
using NorthwindCRUD.Services;

namespace NorthwindCRUD.Controllers
{
[ApiController]
[Route("[controller]")]
public class SalesController : ControllerBase
Expand All @@ -26,7 +26,8 @@ public SalesController(SalesService salesService, IMapper mapper, ILogger logger
[Authorize]
public ActionResult<SalesDto[]> GetSalesByCategoryAndYear([FromQuery] [Required] string categoryName, [FromQuery] int? orderYear = null)
{
try {
try
{
var response = this.salesService.GetSalesDataByCategoryAndYear(categoryName, orderYear);
return Ok(response);
}
Expand All @@ -43,7 +44,6 @@ public ActionResult<SalesDto[]> GetSalesByCountry(
[FromQuery] [Required] string startDate,
[FromQuery] [Required] string endDate)
{

try
{
var salesData = this.salesService.RetrieveSalesDataByCountry(startDate, endDate, country);
Expand Down Expand Up @@ -73,7 +73,6 @@ public ActionResult<SalesDto[]> GetSalesByYear(
[FromQuery] int startMounth,
[FromQuery] int endMounth)
{

try
{
var salesData = this.salesService.RetrieveSalesDataByYear(year, startMounth, endMounth);
Expand Down
1 change: 0 additions & 1 deletion NorthwindCRUD/Controllers/ShippersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public ActionResult<ShipperDto[]> GetAll()
logger.LogError(error.Message);
return StatusCode(500);
}

}

[HttpGet("{id}")]
Expand Down
1 change: 0 additions & 1 deletion NorthwindCRUD/Controllers/SuppliersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public ActionResult<SupplierDto[]> GetAll()
logger.LogError(error.Message);
return StatusCode(500);
}

}

[HttpGet("{id}")]
Expand Down
Loading

0 comments on commit ffc7c06

Please sign in to comment.