Skip to content

Commit

Permalink
Added More custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
trevoirwilliams committed Mar 17, 2022
1 parent ab40b16 commit ce50eb0
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion HotelListing.API.Core/Contracts/IGenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IGenericRepository<T> where T : class
Task<TResult> AddAsync<TSource, TResult>(TSource source);
Task DeleteAsync(int id);
Task UpdateAsync(T entity);
Task UpdateAsync<TSource>(int id, TSource source);
Task UpdateAsync<TSource>(int id, TSource source) where TSource : IBaseDto;
Task<bool> Exists(int id);
}
}
10 changes: 10 additions & 0 deletions HotelListing.API.Core/Exceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HotelListing.API.Core.Exceptions
{
public class BadRequestException : ApplicationException
{
public BadRequestException(string message) : base(message)
{

}
}
}
4 changes: 4 additions & 0 deletions HotelListing.API.Core/Middleware/ExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ private Task HandleExceptionAsync(HttpContext context, Exception ex)
statusCode = HttpStatusCode.NotFound;
errorDetails.ErrorType = "Not Found";
break;
case BadRequestException badRequestException:
statusCode = HttpStatusCode.BadRequest;
errorDetails.ErrorType = "Bad Request";
break;
default:
break;
}
Expand Down
4 changes: 2 additions & 2 deletions HotelListing.API.Core/Models/Country/BaseCountryDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace HotelListing.API.Core.Models.Country
{
public abstract class BaseCountryDto
public abstract class BaseCountryDto : IBaseDto
{
[Required]
public string Name { get; set; }
public string ShortName { get; set; }

public int Id { get; set; }
}
}
1 change: 0 additions & 1 deletion HotelListing.API.Core/Models/Country/CountryDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace HotelListing.API.Core.Models.Country
{
public class CountryDto : BaseCountryDto
{
public int Id { get; set; }
public List<HotelDto> Hotels { get; set; }
}
}
1 change: 0 additions & 1 deletion HotelListing.API.Core/Models/Country/GetCountryDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ namespace HotelListing.API.Core.Models.Country
{
public class GetCountryDto : BaseCountryDto
{
public int Id { get; set; }
}
}
1 change: 0 additions & 1 deletion HotelListing.API.Core/Models/Country/UpdateCountryDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
{
public class UpdateCountryDto : BaseCountryDto
{
public int Id { get; set; }
}
}
3 changes: 2 additions & 1 deletion HotelListing.API.Core/Models/Hotel/BaseHotelDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HotelListing.API.Core.Models.Hotel
{
public abstract class BaseHotelDto
public abstract class BaseHotelDto : IBaseDto
{
[Required]
public string Name { get; set; }
Expand All @@ -15,5 +15,6 @@ public abstract class BaseHotelDto
[Required]
[Range(1, int.MaxValue)]
public int CountryId { get; set; }
public int Id { get; set; }
}
}
1 change: 0 additions & 1 deletion HotelListing.API.Core/Models/Hotel/HotelDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
{
public class HotelDto : BaseHotelDto
{
public int Id { get; set; }
}
}
13 changes: 13 additions & 0 deletions HotelListing.API.Core/Models/IBaseDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelListing.API.Core.Models
{
public interface IBaseDto
{
int Id { get; set; }
}
}
8 changes: 7 additions & 1 deletion HotelListing.API.Core/Repository/GenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Configuration;
using HotelListing.API.Core.Exceptions;
using System.Diagnostics.Metrics;
using HotelListing.API.Core.Models.Hotel;

namespace HotelListing.API.Core.Repository
{
Expand Down Expand Up @@ -115,8 +116,13 @@ public async Task UpdateAsync(T entity)
await _context.SaveChangesAsync();
}

public async Task UpdateAsync<TSource>(int id, TSource source)
public async Task UpdateAsync<TSource>(int id, TSource source) where TSource : IBaseDto
{
if (id != source.Id)
{
throw new BadRequestException("Invalid Id used in request");
}

var entity = await GetAsync(id);

if(entity == null)
Expand Down
5 changes: 0 additions & 5 deletions HotelListing.API/Controllers/CountriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ public async Task<ActionResult<CountryDto>> GetCountry(int id)
[Authorize]
public async Task<IActionResult> PutCountry(int id, UpdateCountryDto updateCountryDto)
{
if (id != updateCountryDto.Id)
{
return BadRequest("Invalid Record Id");
}

try
{
await _countriesRepository.UpdateAsync(id, updateCountryDto);
Expand Down
5 changes: 0 additions & 5 deletions HotelListing.API/Controllers/HotelsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public async Task<ActionResult<HotelDto>> GetHotel(int id)
[HttpPut("{id}")]
public async Task<IActionResult> PutHotel(int id, HotelDto hotelDto)
{
if (id != hotelDto.Id)
{
return BadRequest("Invalid Id used in request");
}

try
{
await _hotelsRepository.UpdateAsync(id, hotelDto);
Expand Down

0 comments on commit ce50eb0

Please sign in to comment.