forked from TWA-AFS-202310/ParkingLotApi
-
Notifications
You must be signed in to change notification settings - Fork 4
Dian Jing's Pull request for parkinglot API #5
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
Open
dianjing104
wants to merge
23
commits into
TWA-AFS-202310-GROUP-4:main
Choose a base branch
from
dianjing104:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6639095
test: add test case for weather forecast controller
1f98e91
feat: add testbase file to refactor code.
60ab399
feat: add create parking lot async
0ffaeb3
refactor : extract capactity check to the service and add DI.
a4d16a6
refactor: add invalid capacity exception
32c5444
refactor: add filter to replace try catch in controller
3155ad2
feat: connect to mongoDB to successfully insert parkinglot to DB
003774f
feat: redesgin DTO and models
19f3875
feat: complete delete parkinglot by id.
5069d69
fix: fix delete input passing way
b7eae22
feat: complete get parkinglot by pagesize and page index
d8464cb
feat: complete get parkinglot by id
24454ac
feat: complete update parkinglot capacity
7ca7bcc
refactor: add invalid capacity exception handler
bf6fb35
fix: add missed files
87eab41
fix: fix constant initialization
ff8e2d8
fix: typo
16eff2d
fix: fix exception error
7abdd6c
fix: change default capacity as constant
bbaff38
fix: fix get parking lot by page
3744c39
feat: add check duplicated name exceptions
aa95700
refactor: remove unnecessary files
f4728be
fix: fix async problem
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,74 @@ | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ParkingLotApi.DTOs; | ||
using ParkingLotApi.Exceptions; | ||
using ParkingLotApi.Models; | ||
using ParkingLotApi.Services; | ||
|
||
namespace ParkingLotApi.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class ParkingLotsController : ControllerBase | ||
{ | ||
private readonly ParkingLotService _parkingLotSaervice; | ||
private readonly int pageSize = 15; | ||
dianjing104 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public ParkingLotsController(ParkingLotService parkingLotService) | ||
{ | ||
this._parkingLotSaervice = parkingLotService; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult<ParkingLotDTO>> CreateParkingLotAsync([FromBody] ParkingLotDTO parkingLotDto) | ||
{ | ||
|
||
return StatusCode(StatusCodes.Status201Created, await _parkingLotSaervice.AddAsync(parkingLotDto)); | ||
|
||
} | ||
|
||
[HttpDelete("{id}")] | ||
public async Task<ActionResult> DeleteParkingLotAsync(string id) | ||
{ | ||
bool isSuccess = _parkingLotSaervice.DeleteAsync(id).Result; | ||
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. 此处采用了阻塞的方法,不好,这样30行的方法不需要声明为 async |
||
if (isSuccess) | ||
{ | ||
return NoContent(); | ||
} | ||
return NotFound(); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult<List<ParkingLot>>> GetParkingLotByPage([FromQuery] int? pageIndex) | ||
{ | ||
|
||
return StatusCode(StatusCodes.Status200OK, | ||
await _parkingLotSaervice.GetParkingLotByPageSizeAsync(pageIndex, pageSize)); | ||
|
||
|
||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<ActionResult<ParkingLot>> GetParkingLotByPage(string id) | ||
dianjing104 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var result = await _parkingLotSaervice.GetParkingLotById(id); | ||
if (result == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return StatusCode(StatusCodes.Status200OK, result); | ||
; } | ||
|
||
[HttpPatch("{id}")] | ||
public async Task<ActionResult<ParkingLot>> UpdateParkingLotAsync(string id, [FromBody] ParkingLotWithCapacityDTO parkingLotWithCapacity) | ||
{ | ||
var result =await _parkingLotSaervice.UpdateParkingLotById(id, parkingLotWithCapacity.Capacity); | ||
if (result == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return StatusCode(StatusCodes.Status200OK, result); | ||
} | ||
} | ||
} |
This file contains hidden or 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 ParkingLotApi.Models; | ||
|
||
namespace ParkingLotApi.DTOs | ||
{ | ||
public class ParkingLotDTO | ||
{ | ||
public string Name { get; set; } | ||
public int Capacity { get; set; } | ||
public string Location { get; set; } | ||
|
||
internal ParkingLot ToEntity() | ||
{ | ||
return new ParkingLot() | ||
{ | ||
Name = Name, | ||
Capacity = Capacity, | ||
Location = Location | ||
}; | ||
} | ||
} | ||
} |
This file contains hidden or 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 ParkingLotApi.DTOs | ||
{ | ||
public class ParkingLotWithCapacityDTO | ||
{ | ||
public int Capacity { get; set; } | ||
} | ||
} |
This file contains hidden or 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 ParkingLotApi.Exceptions | ||
{ | ||
public class InvalidCapacityException : Exception | ||
{ | ||
} | ||
} |
This file contains hidden or 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 ParkingLotApi.Exceptions | ||
{ | ||
public class InvalidPageIndexException: Exception | ||
{ | ||
} | ||
} |
This file contains hidden or 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,29 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using ParkingLotApi.Exceptions; | ||
|
||
namespace ParkingLotApi.Filters | ||
{ | ||
public class InvalidCapacityExceptionFilter : IActionFilter, IOrderedFilter | ||
{ | ||
int IOrderedFilter.Order => int.MaxValue - 10; | ||
void IActionFilter.OnActionExecuted(ActionExecutedContext context) | ||
{ | ||
if (context.Exception is InvalidCapacityException invalidCapacityException) | ||
{ | ||
context.Result = new BadRequestResult(); | ||
context.ExceptionHandled = true; | ||
} | ||
else if (context.Exception is InvalidPageIndexException invalidPageException) | ||
{ | ||
context.Result = new BadRequestResult(); | ||
context.ExceptionHandled = true; | ||
} | ||
} | ||
|
||
void IActionFilter.OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains hidden or 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,17 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
|
||
namespace ParkingLotApi.Models | ||
{ | ||
public class ParkingLot | ||
{ | ||
|
||
[BsonId] [BsonRepresentation(BsonType.ObjectId)] | ||
public string? Id { get; set; } | ||
public string Name { get; set; } | ||
public int Capacity { get; set; } | ||
public string Location { get; set; } | ||
|
||
|
||
} | ||
} |
This file contains hidden or 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 @@ | ||
namespace ParkingLotApi.Models | ||
{ | ||
public class ParkinglotDatabaseSettings | ||
{ | ||
public string ConnectionString { get; set; } = null; | ||
public string DatabaseName { get; set; } = null; | ||
public string CollectionName { get; set; } = null; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 ParkingLotApi.DTOs; | ||
using ParkingLotApi.Models; | ||
|
||
namespace ParkingLotApi.Repositories | ||
{ | ||
public interface IParkingLotRepository | ||
{ | ||
public Task<ParkingLot> CreateParkingLot(ParkingLot parkingLot); | ||
public Task<bool> DeleteParkingLot(string id); | ||
public Task<List<ParkingLot>> GetParkingLot(); | ||
public Task<ParkingLot> GetParkingLotById(string id); | ||
public Task<ParkingLot> UpdateParkingLotById(string id, int capacity); | ||
} | ||
} |
This file contains hidden or 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,52 @@ | ||
using Microsoft.Extensions.Options; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using ParkingLotApi.Models; | ||
using System.Security.Cryptography; | ||
|
||
namespace ParkingLotApi.Repositories | ||
{ | ||
public class ParkingLotRepository:IParkingLotRepository | ||
{ | ||
private readonly IMongoCollection<ParkingLot> _parkingLotCollection; | ||
|
||
public ParkingLotRepository(IOptions<ParkinglotDatabaseSettings> parkingLotDatabaseSetting) | ||
{ | ||
var mongoClient = new MongoClient(parkingLotDatabaseSetting.Value.ConnectionString); | ||
var mongoDatabase = mongoClient.GetDatabase(parkingLotDatabaseSetting.Value.DatabaseName); | ||
_parkingLotCollection = | ||
mongoDatabase.GetCollection<ParkingLot>(parkingLotDatabaseSetting.Value.CollectionName); | ||
} | ||
|
||
public async Task<ParkingLot> CreateParkingLot(ParkingLot parkinglot) | ||
{ | ||
await _parkingLotCollection.InsertOneAsync(parkinglot); | ||
return await _parkingLotCollection.Find(parking => parking.Id == parkinglot.Id).FirstAsync(); | ||
} | ||
|
||
public async Task<bool> DeleteParkingLot(string id) | ||
{ | ||
var result = _parkingLotCollection.DeleteOneAsync(parkinglot => parkinglot.Id == id).Result; | ||
return result.DeletedCount > 0 ; | ||
|
||
} | ||
|
||
public async Task<List<ParkingLot>> GetParkingLot() | ||
{ | ||
return await _parkingLotCollection.Find(_ => true).ToListAsync(); | ||
} | ||
|
||
public async Task<ParkingLot> GetParkingLotById(string id) | ||
{ | ||
return await _parkingLotCollection.Find(parkingLot => parkingLot.Id == id).FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<ParkingLot> UpdateParkingLotById(string id, int capacity) | ||
{ | ||
var update = Builders<ParkingLot>.Update.Set("Capacity", 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. 也可以用builder |
||
await _parkingLotCollection.UpdateOneAsync(parkingLot => parkingLot.Id == id, | ||
update); | ||
return await _parkingLotCollection.Find(parkingLot => parkingLot.Id == id).FirstOrDefaultAsync(); | ||
} | ||
} | ||
} |
This file contains hidden or 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,62 @@ | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using ParkingLotApi.DTOs; | ||
using ParkingLotApi.Exceptions; | ||
using ParkingLotApi.Models; | ||
using ParkingLotApi.Repositories; | ||
|
||
namespace ParkingLotApi.Services | ||
{ | ||
public class ParkingLotService | ||
{ | ||
private readonly IParkingLotRepository _parkingLotRepository; | ||
public ParkingLotService(IParkingLotRepository parkingLotRepository) | ||
{ | ||
this._parkingLotRepository = parkingLotRepository; | ||
} | ||
public async Task<ParkingLot> AddAsync(ParkingLotDTO parkingLotDto) | ||
{ | ||
if (parkingLotDto.Capacity < 10) | ||
{ | ||
throw new InvalidCapacityException(); | ||
} | ||
|
||
return await _parkingLotRepository.CreateParkingLot(parkingLotDto.ToEntity()); | ||
} | ||
|
||
public async Task<bool> DeleteAsync(string id) | ||
{ | ||
return await _parkingLotRepository.DeleteParkingLot(id); | ||
} | ||
|
||
public async Task<List<ParkingLot>> GetParkingLotByPageSizeAsync(int? pageIndex, int pageSize) | ||
{ | ||
if (pageIndex <= 0 || pageIndex == null) | ||
{ | ||
throw new InvalidCapacityException(); | ||
dianjing104 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
List<ParkingLot> parkingLots = await _parkingLotRepository.GetParkingLot(); | ||
dianjing104 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int pageCount = parkingLots.Count / pageSize; | ||
pageCount = parkingLots.Count % pageSize == 0? pageCount: pageCount+=1; | ||
if ( pageSize <= parkingLots.Count && pageIndex > pageCount ) | ||
{ | ||
throw new InvalidPageIndexException(); | ||
} | ||
|
||
return parkingLots.Skip(((int)pageIndex - 1) * (int)pageSize).Take((int)pageSize).ToList(); | ||
} | ||
|
||
public async Task<ParkingLot> GetParkingLotById(string id) | ||
{ | ||
return await _parkingLotRepository.GetParkingLotById(id); | ||
} | ||
|
||
public async Task<ParkingLot> UpdateParkingLotById(string id, int capacity) | ||
{ | ||
if (capacity < 10) | ||
dianjing104 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw new InvalidCapacityException(); | ||
} | ||
return await _parkingLotRepository.UpdateParkingLotById(id, capacity); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
ParkingLotApiTest/Controllers/ParkingLotsControllerTest.cs
This file contains hidden or 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,34 @@ | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using ParkingLotApi; | ||
using ParkingLotApi.DTOs; | ||
|
||
namespace ParkingLotApiTest.Controllers | ||
{ | ||
public class ParkingLotsControllerTest : TestBase | ||
{ | ||
public ParkingLotsControllerTest(WebApplicationFactory<Program> webApplicationFactory) : base(webApplicationFactory) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task Should_return_bad_request_correctly_when_post_given_capacity_less_10() | ||
{ | ||
//given | ||
HttpClient _httpClient = GetClient(); | ||
ParkingLotDTO parkinglotDtoWithCapacity1 = new ParkingLotDTO() | ||
{ | ||
Name = "name", | ||
Capacity = 1, | ||
Location = "S", | ||
}; | ||
|
||
//when | ||
HttpResponseMessage response = await _httpClient.PostAsJsonAsync("/parkinglots", parkinglotDtoWithCapacity1); | ||
|
||
//then | ||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.