Skip to content

Commit

Permalink
Added Hotels API Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
trevoirwilliams committed Mar 9, 2022
1 parent 01ad6d4 commit 81be882
Show file tree
Hide file tree
Showing 35 changed files with 1,163 additions and 29 deletions.
21 changes: 21 additions & 0 deletions HotelListing.API.NET5/Configurations/MapperConfig.cs
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();

}
}
}
10 changes: 10 additions & 0 deletions HotelListing.API.NET5/Contracts/ICountriesRepository.cs
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);
}
}
14 changes: 14 additions & 0 deletions HotelListing.API.NET5/Contracts/IGenericRepository.cs
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 HotelListing.API.NET5/Controllers/CountriesController.cs
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);
}
}
}
16 changes: 5 additions & 11 deletions HotelListing.API.NET5/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace HotelListing.API.NET5.Controllers
{
Expand All @@ -23,17 +18,16 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger)
_logger = logger;
}

[HttpGet]
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
}
12 changes: 12 additions & 0 deletions HotelListing.API.NET5/Data/Country.cs
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; }
}
}
16 changes: 16 additions & 0 deletions HotelListing.API.NET5/Data/Hotel.cs
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; }
}
}
67 changes: 67 additions & 0 deletions HotelListing.API.NET5/Data/HotelListingDbContext.cs
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
}
);
}
}
}
17 changes: 16 additions & 1 deletion HotelListing.API.NET5/HotelListing.API.NET5.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.15">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Expressions" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Expressions" Version="3.3.0" />


<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.15" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 81be882

Please sign in to comment.