forked from trevoirwilliams/HotelListing.API.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REST API Development Best Practices. Automapper and Repositories
- Loading branch information
1 parent
93e0f5a
commit 01ad6d4
Showing
14 changed files
with
228 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using AutoMapper; | ||
using HotelListing.API.Data; | ||
using HotelListing.API.Models.Country; | ||
using HotelListing.API.Models.Hotel; | ||
|
||
namespace HotelListing.API.Configurations | ||
{ | ||
public class MapperConfig : Profile | ||
{ | ||
public MapperConfig() | ||
{ | ||
CreateMap<Country, CreateCountryDto>().ReverseMap(); | ||
CreateMap<Country, GetCountryDto>().ReverseMap(); | ||
CreateMap<Country, CountryDto>().ReverseMap(); | ||
CreateMap<Country, UpdateCountryDto>().ReverseMap(); | ||
|
||
CreateMap<Hotel, HotelDto>().ReverseMap(); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using HotelListing.API.Data; | ||
|
||
namespace HotelListing.API.Contracts | ||
{ | ||
public interface ICountriesRepository : IGenericRepository<Country> | ||
{ | ||
Task<Country> GetDetails(int id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace HotelListing.API.Contracts | ||
{ | ||
public interface IGenericRepository<T> where T : class | ||
{ | ||
Task<T> GetAsync(int? id); | ||
Task<List<T>> GetAllAsync(); | ||
Task<T> AddAsync(T entity); | ||
Task DeleteAsync(int id); | ||
Task UpdateAsync(T entity); | ||
Task<bool> Exists(int id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HotelListing.API.Models.Country | ||
{ | ||
public abstract class BaseCountryDto | ||
{ | ||
[Required] | ||
public string Name { get; set; } | ||
public string ShortName { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using HotelListing.API.Models.Hotel; | ||
|
||
namespace HotelListing.API.Models.Country | ||
{ | ||
public class CountryDto : BaseCountryDto | ||
{ | ||
public int Id { get; set; } | ||
public List<HotelDto> Hotels { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HotelListing.API.Models.Country | ||
{ | ||
public class CreateCountryDto : BaseCountryDto | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace HotelListing.API.Models.Country | ||
{ | ||
public class GetCountryDto : BaseCountryDto | ||
{ | ||
public int Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HotelListing.API.Models.Country | ||
{ | ||
public class UpdateCountryDto : BaseCountryDto | ||
{ | ||
public int Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace HotelListing.API.Models.Hotel | ||
{ | ||
public class HotelDto | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string Address { get; set; } | ||
public double Rating { get; set; } | ||
public int CountryId { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using HotelListing.API.Contracts; | ||
using HotelListing.API.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HotelListing.API.Repository | ||
{ | ||
public class CountriesRepository : GenericRepository<Country>, ICountriesRepository | ||
{ | ||
private readonly HotelListingDbContext _context; | ||
|
||
public CountriesRepository(HotelListingDbContext context) : base(context) | ||
{ | ||
this._context = context; | ||
} | ||
|
||
public async Task<Country> GetDetails(int id) | ||
{ | ||
return await _context.Countries.Include(q => q.Hotels) | ||
.FirstOrDefaultAsync(q => q.Id == id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using HotelListing.API.Contracts; | ||
using HotelListing.API.Data; | ||
using HotelListing.API.Models.Country; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace HotelListing.API.Repository | ||
{ | ||
public class GenericRepository<T> : IGenericRepository<T> where T : class | ||
{ | ||
private readonly HotelListingDbContext _context; | ||
|
||
public GenericRepository(HotelListingDbContext context) | ||
{ | ||
this._context = context; | ||
} | ||
|
||
public async Task<T> AddAsync(T entity) | ||
{ | ||
await _context.AddAsync(entity); | ||
await _context.SaveChangesAsync(); | ||
return entity; | ||
} | ||
|
||
public async Task DeleteAsync(int id) | ||
{ | ||
var entity = await GetAsync(id); | ||
_context.Set<T>().Remove(entity); | ||
await _context.SaveChangesAsync(); | ||
} | ||
|
||
public async Task<bool> Exists(int id) | ||
{ | ||
var entity = await GetAsync(id); | ||
return entity != null; | ||
} | ||
|
||
public async Task<List<T>> GetAllAsync() | ||
{ | ||
return await _context.Set<T>().ToListAsync(); | ||
} | ||
|
||
public async Task<T> GetAsync(int? id) | ||
{ | ||
if (id is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return await _context.Set<T>().FindAsync(id); | ||
} | ||
|
||
public async Task UpdateAsync(T entity) | ||
{ | ||
_context.Update(entity); | ||
await _context.SaveChangesAsync(); | ||
} | ||
} | ||
} |