-
Notifications
You must be signed in to change notification settings - Fork 3
WangKe's HW #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
WangKe's HW #4
Changes from all commits
3928705
aa1db5b
c4583ae
6b4be14
00db1cc
b7254ab
f3fec77
127ef17
b36fd7c
e33202f
8cd4802
d6efba8
36dd13a
af87bad
ca1c87f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using ParkingLotApi.Exceptions; | ||
| using ParkingLotApi.Models; | ||
| using ParkingLotApi.Services; | ||
|
|
||
| namespace ParkingLotApi.Controllers | ||
| { | ||
| [ApiController] | ||
| [Route("api/[controller]")] | ||
| public class ParkingLotsController : ControllerBase | ||
| { | ||
| private readonly ParkingLotsService _parkingLotsService; | ||
| public ParkingLotsController(ParkingLotsService parkingLotsService) | ||
| { | ||
| _parkingLotsService = parkingLotsService; | ||
| } | ||
|
|
||
| [HttpPost] | ||
| public async Task<ActionResult<ParkingLotEntity>> AddParkingLot([FromBody] ParkingLotDto parkingLotDto) | ||
| { | ||
| /* try | ||
| {*/ | ||
| // return Created("", await _parkingLotsService.AddAsync(parkingLotDto)); | ||
| return StatusCode(StatusCodes.Status201Created, await _parkingLotsService.AddAsync(parkingLotDto)); | ||
| /* } catch (InvalidCapacityException ex) | ||
| { | ||
| return BadRequest(ex.Message); | ||
| }*/ | ||
| } | ||
|
|
||
| [HttpDelete("{id}")] | ||
| public async Task<ActionResult> RemoveParkingLot(string id) | ||
| { | ||
| var isRemoved = await _parkingLotsService.RemoveAsync(id); | ||
| if (isRemoved) | ||
| { | ||
| return NoContent(); | ||
| } | ||
| return NotFound(); | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public async Task<ActionResult<List<ParkingLotEntity>>> GetOnePageParkingLots([FromQuery] int pageIndex) | ||
| { | ||
| return await _parkingLotsService.GetOnePageParkingLots(pageIndex); | ||
| } | ||
|
|
||
| [HttpGet("{id}")] | ||
| public async Task<ActionResult<List<ParkingLotEntity>>> GetParkingLotById(string id) | ||
| { | ||
| var parkingLot = await _parkingLotsService.GetByIdAsync(id); | ||
| if (parkingLot == null) | ||
| { | ||
| return NotFound(); | ||
| } | ||
| return Ok(parkingLot); | ||
| } | ||
|
|
||
| [HttpPut("{id}")] | ||
| public async Task<ActionResult<ParkingLotEntity>> UpdateCapacityAsync(string id, [FromBody] int capacity) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: use class for request body instead of int |
||
| { | ||
| var updatedParkingLot = await _parkingLotsService.UpdateCapacity(id, capacity); | ||
| if (updatedParkingLot == null) | ||
| { | ||
| return NotFound(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: this can also be handled in global exception handler |
||
| } | ||
| return Ok(updatedParkingLot); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| namespace ParkingLotApi.Models | ||
| { | ||
| public class ParkingLotDto | ||
| { | ||
| public string Name { get; set; } | ||
| public int Capacity { get; set; } | ||
| public string Location { get; set; } | ||
|
|
||
| internal ParkingLotEntity ToEntity() | ||
| { | ||
| return new ParkingLotEntity { Name = Name, Capacity = Capacity, Location = Location }; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace ParkingLotApi.Exceptions | ||
| { | ||
| public class InvalidCapacityException : Exception | ||
| { | ||
| public InvalidCapacityException(string message) : base(message) | ||
| { | ||
|
|
||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using Microsoft.AspNetCore.Http.HttpResults; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.Filters; | ||
| using ParkingLotApi.Exceptions; | ||
|
|
||
| namespace ParkingLotApi.Filters | ||
| { | ||
| public class InvalidCapacityExceptionFilter : IActionFilter, IOrderedFilter | ||
| { | ||
| public int Order => int.MaxValue - 10; | ||
|
|
||
| public void OnActionExecuted(ActionExecutedContext context) | ||
| { | ||
| if (context.Exception is InvalidCapacityException invalidCapacityException) | ||
| { | ||
| context.Result = new BadRequestResult(); | ||
| context.ExceptionHandled = true; | ||
| } | ||
| } | ||
|
|
||
| public void OnActionExecuting(ActionExecutingContext context) | ||
| { | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using MongoDB.Bson; | ||
| using MongoDB.Bson.Serialization.Attributes; | ||
|
|
||
| namespace ParkingLotApi.Models | ||
| { | ||
| public class ParkingLotEntity | ||
| { | ||
| [BsonId] | ||
| [BsonRepresentation(BsonType.ObjectId)] | ||
| public string? Id { get; set; } | ||
|
|
||
| public string Name { get; set; } | ||
| public int Capacity { get; set; } | ||
| public string Location { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace ParkingLotApi.Models | ||
| { | ||
| public class ParkingLotsDatabaseSettings | ||
| { | ||
| public string ConnectionString { get; set; } = null!; | ||
| public string DatabaseName { get; set; } = null!; | ||
| public string CollectionName { get; set; } = null!; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using ParkingLotApi.Models; | ||
|
|
||
| namespace ParkingLotApi.Repositories | ||
| { | ||
| public interface IParkingLotsRepository | ||
| { | ||
| Task<ParkingLotEntity> CreateParkingLotAsync(ParkingLotEntity parkingLo); | ||
| Task<bool> DeleteParkingLotAsync(string id); | ||
| Task<List<ParkingLotEntity>> GetAllAsync(); | ||
| Task<ParkingLotEntity> GetByIdAsync(string id); | ||
| Task<ParkingLotEntity> UpdateCapacity(string id, ParkingLotEntity updatedParkingLot); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using Microsoft.Extensions.Options; | ||
| using MongoDB.Driver; | ||
| using ParkingLotApi.Models; | ||
|
|
||
| namespace ParkingLotApi.Repositories | ||
| { | ||
|
|
||
| public class ParkingLotsRepository : IParkingLotsRepository | ||
| { | ||
| private readonly IMongoCollection<ParkingLotEntity> _parkingLotsCollection; | ||
| public ParkingLotsRepository(IOptions<ParkingLotsDatabaseSettings> parkingLotsDatabaseSettings) | ||
| { | ||
| var mongoClient = new MongoClient(parkingLotsDatabaseSettings.Value.ConnectionString); | ||
| var mongoDatabase = mongoClient.GetDatabase(parkingLotsDatabaseSettings.Value.DatabaseName); | ||
| _parkingLotsCollection = mongoDatabase.GetCollection<ParkingLotEntity>(parkingLotsDatabaseSettings.Value.CollectionName); | ||
|
|
||
| } | ||
|
|
||
| public async Task<ParkingLotEntity> CreateParkingLotAsync(ParkingLotEntity parkingLot) | ||
| { | ||
| await _parkingLotsCollection.InsertOneAsync(parkingLot); | ||
| return await _parkingLotsCollection.Find(a => a.Name == parkingLot.Name).FirstAsync(); | ||
| } | ||
|
|
||
| public async Task<bool> DeleteParkingLotAsync(string id) | ||
| { | ||
| var result = await _parkingLotsCollection.DeleteOneAsync(p => p.Id == id); | ||
| return result.DeletedCount > 0; | ||
| } | ||
|
|
||
| public async Task<List<ParkingLotEntity>> GetAllAsync() | ||
| { | ||
| return await _parkingLotsCollection.Find(_ => true).ToListAsync(); | ||
| } | ||
|
|
||
| public async Task<ParkingLotEntity> GetByIdAsync(string id) | ||
| { | ||
| return await _parkingLotsCollection.Find(p => p.Id == id).FirstOrDefaultAsync(); | ||
| } | ||
|
|
||
| public async Task<ParkingLotEntity?> UpdateCapacity(string id, ParkingLotEntity updatedParkingLot) | ||
| { | ||
| await _parkingLotsCollection.ReplaceOneAsync(p => p.Id == id, updatedParkingLot); | ||
| return await GetByIdAsync(id); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using ParkingLotApi.Exceptions; | ||
| using ParkingLotApi.Models; | ||
| using ParkingLotApi.Repositories; | ||
| using System; | ||
|
|
||
| namespace ParkingLotApi.Services | ||
| { | ||
| public class ParkingLotsService | ||
| { | ||
| private readonly IParkingLotsRepository _parkingLotsRepository; | ||
| public ParkingLotsService(IParkingLotsRepository parkingLotsRepository) { | ||
| _parkingLotsRepository = parkingLotsRepository; | ||
| } | ||
| public async Task<ParkingLotEntity> AddAsync(ParkingLotDto parkingLotDto) | ||
| { | ||
| if (parkingLotDto.Capacity < 10) | ||
| { | ||
| throw new InvalidCapacityException("The capacity should be no less than 10"); | ||
| } | ||
| return await _parkingLotsRepository.CreateParkingLotAsync(parkingLotDto.ToEntity()); | ||
| } | ||
|
|
||
| public async Task<bool> RemoveAsync(string id) | ||
| { | ||
| return await _parkingLotsRepository.DeleteParkingLotAsync(id); | ||
| } | ||
|
|
||
| public async Task<List<ParkingLotEntity>> GetOnePageParkingLots(int pageIndex) | ||
| { | ||
| var allParkingLots = await _parkingLotsRepository.GetAllAsync(); | ||
| int pageSize = 15; | ||
| int startIdx = (int)((pageIndex - 1) * pageSize); | ||
| return allParkingLots.Skip(startIdx).Take((int)pageSize).ToList(); | ||
| } | ||
|
|
||
| public async Task<ParkingLotEntity> GetByIdAsync(string id) | ||
| { | ||
| return await _parkingLotsRepository.GetByIdAsync(id); | ||
| } | ||
|
|
||
| public IParkingLotsRepository Get_parkingLotsRepository() | ||
| { | ||
| return _parkingLotsRepository; | ||
| } | ||
|
|
||
| public async Task<ParkingLotEntity?> UpdateCapacity(string id, int capacity) | ||
| { | ||
| var parkingLot = await _parkingLotsRepository.GetByIdAsync(id); | ||
| if (parkingLot != null) | ||
| { | ||
| var updatedParkingLot = new ParkingLotEntity() | ||
| { | ||
| Capacity = capacity, | ||
| Id = parkingLot.Id, | ||
| Name = parkingLot.Name, | ||
| Location = parkingLot.Location | ||
| }; | ||
| return await _parkingLotsRepository.UpdateCapacity(id, updatedParkingLot); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: avoid return null from method, since null is hard to understand. here we can throw exception from service