-
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.
Paging & Refactoring Code with Bookings CRUD Implemented
- Loading branch information
1 parent
0b78deb
commit f63e22e
Showing
10 changed files
with
231 additions
and
10 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,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 Hotels.DataAccess.Data; | ||
using Hotels.Models.Models; | ||
|
||
namespace Hotels.API.Controllers; | ||
|
||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class BookingsController : ControllerBase | ||
{ | ||
private readonly DataContext _context; | ||
|
||
public BookingsController(DataContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
// GET: api/Bookings | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<Booking>>> GetBookings() | ||
{ | ||
if (_context.Bookings == null) | ||
{ | ||
return NotFound(); | ||
} | ||
return await _context.Bookings.ToListAsync(); | ||
} | ||
|
||
// GET: api/Bookings/5 | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<Booking>> GetBooking(int id) | ||
{ | ||
if (_context.Bookings == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var booking = await _context.Bookings.FindAsync(id); | ||
|
||
if (booking == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return booking; | ||
} | ||
|
||
// PUT: api/Bookings/5 | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPut("{id}")] | ||
public async Task<IActionResult> PutBooking(int id, Booking booking) | ||
{ | ||
if (id != booking.Id) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
_context.Entry(booking).State = EntityState.Modified; | ||
|
||
try | ||
{ | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!BookingExists(id)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
return NoContent(); | ||
} | ||
|
||
// POST: api/Bookings | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPost] | ||
public async Task<ActionResult<Booking>> PostBooking(Booking booking) | ||
{ | ||
if (_context.Bookings == null) | ||
{ | ||
return Problem("Entity set 'DataContext.Bookings' is null."); | ||
} | ||
_context.Bookings.Add(booking); | ||
await _context.SaveChangesAsync(); | ||
|
||
return CreatedAtAction("GetBooking", new { id = booking.Id }, booking); | ||
} | ||
|
||
// DELETE: api/Bookings/5 | ||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> DeleteBooking(int id) | ||
{ | ||
if (_context.Bookings == null) | ||
{ | ||
return NotFound(); | ||
} | ||
var booking = await _context.Bookings.FindAsync(id); | ||
if (booking == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
_context.Bookings.Remove(booking); | ||
await _context.SaveChangesAsync(); | ||
|
||
return NoContent(); | ||
} | ||
|
||
private bool BookingExists(int id) | ||
{ | ||
return (_context.Bookings?.Any(e => e.Id == id)).GetValueOrDefault(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using Hotels.API.Exceptions; | ||
using Hotels.Models.Exceptions; | ||
using Newtonsoft.Json; | ||
using System.Net; | ||
|
||
|
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using Hotels.Models.Models; | ||
using Hotels.Models.Dtos.City; | ||
using Hotels.Models.Models; | ||
|
||
namespace Hotels.DataAccess.Contracts; | ||
|
||
public interface ICitiesRepository : IGenericRepository<City> | ||
{ | ||
Task<City> GetDetails(int id); | ||
Task<CityDto> 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 |
---|---|---|
@@ -1,11 +1,20 @@ | ||
namespace Hotels.DataAccess.Contracts; | ||
using Hotels.Models; | ||
using Hotels.Models.Models.QueryResponse; | ||
using Hotels.Models.Models.Response; | ||
|
||
namespace Hotels.DataAccess.Contracts; | ||
|
||
public interface IGenericRepository<T> where T : class | ||
{ | ||
Task<T> GetAsync(int? id); | ||
Task<TResult> GetAsync<TResult>(int? id); | ||
Task<IEnumerable<T>> GetAllAsync(); | ||
Task<List<TResult>> GetAllAsync<TResult>(); | ||
Task<PagedResult<TResult>> GetAllAsync<TResult>(QueryParameters queryParameters); | ||
Task<T> AddAsync(T entity); | ||
Task<TResult> AddAsync<TSource, TResult>(TSource source); | ||
Task DeleteAsync(int id); | ||
Task UpdateAsync(T entity); | ||
Task UpdateAsync<TSource>(int id, TSource source) where TSource : IBase; | ||
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
2 changes: 1 addition & 1 deletion
2
Hotels.API/Exceptions/BadRequestException.cs → ....Models/Exceptions/BadRequestException.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
2 changes: 1 addition & 1 deletion
2
Hotels.API/Exceptions/NotFoundException.cs → ...ls.Models/Exceptions/NotFoundException.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
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,6 @@ | ||
namespace Hotels.Models; | ||
|
||
public interface IBase | ||
{ | ||
int Id { get; set; } | ||
} |