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.
- Loading branch information
1 parent
01ad6d4
commit 81be882
Showing
35 changed files
with
1,163 additions
and
29 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.NET5.Data; | ||
using HotelListing.API.NET5.Models.Country; | ||
using HotelListing.API.NET5.Models.Hotel; | ||
|
||
namespace HotelListing.API.NET5.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,10 @@ | ||
using HotelListing.API.NET5.Data; | ||
using System.Threading.Tasks; | ||
|
||
namespace HotelListing.API.NET5.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,14 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
namespace HotelListing.API.NET5.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); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
HotelListing.API.NET5/Controllers/CountriesController.cs
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,123 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using HotelListing.API.NET5.Data; | ||
using HotelListing.API.NET5.Models.Country; | ||
using AutoMapper; | ||
using HotelListing.API.NET5.Contracts; | ||
|
||
namespace HotelListing.API.NET5.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class CountriesController : ControllerBase | ||
{ | ||
private readonly IMapper _mapper; | ||
private readonly ICountriesRepository _countriesRepository; | ||
|
||
public CountriesController(IMapper mapper, ICountriesRepository countriesRepository) | ||
{ | ||
this._mapper = mapper; | ||
this._countriesRepository = countriesRepository; | ||
} | ||
|
||
// GET: api/Countries | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<GetCountryDto>>> GetCountries() | ||
{ | ||
var countries = await _countriesRepository.GetAllAsync(); | ||
var records = _mapper.Map<List<GetCountryDto>>(countries); | ||
return Ok(records); | ||
} | ||
|
||
// GET: api/Countries/5 | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<CountryDto>> GetCountry(int id) | ||
{ | ||
var country = await _countriesRepository.GetDetails(id); | ||
|
||
if (country == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var countryDto = _mapper.Map<CountryDto>(country); | ||
|
||
return Ok(countryDto); | ||
} | ||
|
||
// PUT: api/Countries/5 | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPut("{id}")] | ||
public async Task<IActionResult> PutCountry(int id, UpdateCountryDto updateCountryDto) | ||
{ | ||
if (id != updateCountryDto.Id) | ||
{ | ||
return BadRequest("Invalid Record Id"); | ||
} | ||
|
||
var country = await _countriesRepository.GetAsync(id); | ||
|
||
if (country == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
_mapper.Map(updateCountryDto, country); | ||
|
||
try | ||
{ | ||
await _countriesRepository.UpdateAsync(country); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!await CountryExists(id)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
return NoContent(); | ||
} | ||
|
||
// POST: api/Countries | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPost] | ||
public async Task<ActionResult<Country>> PostCountry(CreateCountryDto createCountryDto) | ||
{ | ||
var country = _mapper.Map<Country>(createCountryDto); | ||
|
||
await _countriesRepository.AddAsync(country); | ||
|
||
return CreatedAtAction("GetCountry", new { id = country.Id }, country); | ||
} | ||
|
||
// DELETE: api/Countries/5 | ||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> DeleteCountry(int id) | ||
{ | ||
var country = await _countriesRepository.GetAsync(id); | ||
if (country == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
await _countriesRepository.DeleteAsync(id); | ||
|
||
return NoContent(); | ||
} | ||
|
||
private async Task<bool> CountryExists(int id) | ||
{ | ||
return await _countriesRepository.Exists(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace HotelListing.API.NET5.Data | ||
{ | ||
public class Country | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string ShortName { get; set; } | ||
|
||
|
||
public virtual IList<Hotel> 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,16 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace HotelListing.API.NET5.Data | ||
{ | ||
public class Hotel | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string Address { get; set; } | ||
public double Rating { get; set; } | ||
|
||
[ForeignKey(nameof(CountryId))] | ||
public int CountryId { get; set; } | ||
public Country Country { 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,67 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HotelListing.API.NET5.Data | ||
{ | ||
public class HotelListingDbContext : DbContext | ||
{ | ||
public HotelListingDbContext(DbContextOptions options) : base(options) | ||
{ | ||
|
||
} | ||
|
||
public DbSet<Hotel> Hotels { get; set; } | ||
public DbSet<Country> Countries { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
modelBuilder.Entity<Country>().HasData( | ||
new Country | ||
{ | ||
Id = 1, | ||
Name = "Jamaica", | ||
ShortName = "JM" | ||
}, | ||
new Country | ||
{ | ||
Id = 2, | ||
Name = "Bahamas", | ||
ShortName = "BS" | ||
}, | ||
new Country | ||
{ | ||
Id = 3, | ||
Name = "Cayman Island", | ||
ShortName = "CI" | ||
} | ||
); | ||
|
||
modelBuilder.Entity<Hotel>().HasData( | ||
new Hotel | ||
{ | ||
Id = 1, | ||
Name = "Sandals Resort and Spa", | ||
Address = "Negril", | ||
CountryId = 1, | ||
Rating = 4.5 | ||
}, | ||
new Hotel | ||
{ | ||
Id = 2, | ||
Name = "Comfort Suites", | ||
Address = "George Town", | ||
CountryId = 3, | ||
Rating = 4.3 | ||
}, | ||
new Hotel | ||
{ | ||
Id = 3, | ||
Name = "Grand Palldium", | ||
Address = "Nassua", | ||
CountryId = 2, | ||
Rating = 4 | ||
} | ||
); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.