forked from TWA-AFS-202310/ParkingLotApi
-
Notifications
You must be signed in to change notification settings - Fork 4
upload parking lot controller implement #6
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
LucasCyw
wants to merge
15
commits into
TWA-AFS-202310-GROUP-4:main
Choose a base branch
from
LucasCyw: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
15 commits
Select commit
Hold shift + click to select a range
6dc934a
refactor: refact the weatherforest controller test
c7e3fb3
feat: partial implement parkinglot controller
8f1ddb5
refactor: extract capacity validation logic to service
faa0c15
refactor: refact exception message
6e5cd83
refactor: refact exception message
68e664e
feat: implement delete method
6cb15c5
feat: implement get parkinglot with pageIndex
309eb53
feat: implement get parkinglot by id
efc6099
feat: implement patch parkinglot by id with partial request parkinglo…
d6ab763
feat: implement return 404 when get parkinglot by id and id is not exist
ff9174f
fix: patch capacity is invalid value should throw exception
fc04477
fix: patch capacity is invalid value should throw exception
9b4c95b
fix: add duplicate name check and change correct statuscode
bfe8a93
fix: modify find all load all data in mem
8bad45b
fix: check patch method id is existed
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,26 @@ | ||
URL: parkinglots | ||
|
||
Method: Post | ||
|
||
request body: | ||
|
||
"""json | ||
{ | ||
name: "post", | ||
capacity: 10, | ||
location: "hh" | ||
} | ||
""" | ||
|
||
response code: httpstatuscode.created / badrequest | ||
|
||
response body: | ||
|
||
"""json | ||
{ | ||
id: guid | ||
name: "post", | ||
capacity: 10, | ||
location: "hh" | ||
} | ||
""" | ||
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. Well: Api Doc included |
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,65 @@ | ||
using Microsoft.AspNetCore.JsonPatch; | ||
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 _service; | ||
public ParkingLotsController(ParkingLotService service) | ||
{ | ||
this._service = service; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult<ParkingLotDto>> AddParkingLotAsync([FromBody] ParkingLotDto data) | ||
{ | ||
return StatusCode(StatusCodes.Status201Created, await _service.AddAsync(data)); | ||
} | ||
|
||
|
||
[HttpDelete("{id}")] | ||
public async Task<ActionResult> DeleteParkingLotAsync(string id) | ||
{ | ||
await _service.DeleteAsync(id); | ||
return NoContent(); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult<List<ParkingLot>>> GetPartialAsync(int? pageSize = 15, int? pageIndex = 0) | ||
{ | ||
return StatusCode(StatusCodes.Status200OK, await _service.GetPartialAsync((int)pageSize, (int)pageIndex)); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<ActionResult<ParkingLot>> GetbyIdAsync(string id) | ||
{ | ||
return StatusCode(StatusCodes.Status200OK, await _service.GetByIdAsync(id)); | ||
} | ||
|
||
[HttpPatch("{id}")] | ||
public async Task<ActionResult<ParkingLot>> UpdatebyIdAsync(string id, [FromBody] JsonPatchDocument<ParkingLotDto> patchDoc) | ||
{ | ||
if (patchDoc != null) | ||
{ | ||
var parkingLotDto = new ParkingLotDto(); | ||
patchDoc.ApplyTo(parkingLotDto, ModelState); | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
return StatusCode(StatusCodes.Status200OK, await _service.UpdateByIdAsync(id, parkingLotDto)); | ||
} | ||
else | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
using ParkingLotApi.Models; | ||
|
||
namespace ParkingLotApi.Dtos | ||
{ | ||
public class ParkingLotDto | ||
{ | ||
public string Name { get; set; } | ||
public int Capacity { get; set; } | ||
public string Location { get; set; } | ||
|
||
public 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,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 InvalidIdException : 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 InvalidNameException : 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,25 @@ | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using ParkingLotApi.Exceptions; | ||
|
||
namespace ParkingLotApi.Filter | ||
{ | ||
public class InvalidCapacityExceptionFilter : IActionFilter, IOrderedFilter | ||
{ | ||
public int Order => int.MaxValue - 10; | ||
|
||
public void OnActionExecuted(ActionExecutedContext context) // when controller method return | ||
{ | ||
if (context.Exception is InvalidCapacityException) | ||
{ | ||
context.Result = new BadRequestResult(); | ||
context.ExceptionHandled = true; | ||
} | ||
} | ||
|
||
public void OnActionExecuting(ActionExecutingContext context) // before controller method run | ||
{ | ||
} | ||
} | ||
} |
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,24 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ParkingLotApi.Exceptions; | ||
|
||
namespace ParkingLotApi.Filter | ||
{ | ||
public class InvalidIdExceptionFilter : IActionFilter, IOrderedFilter | ||
{ | ||
public int Order => int.MaxValue - 10; | ||
|
||
public void OnActionExecuted(ActionExecutedContext context) // when controller method return | ||
{ | ||
if (context.Exception is InvalidIdException) | ||
{ | ||
context.Result = new NotFoundResult(); | ||
context.ExceptionHandled = true; | ||
} | ||
} | ||
|
||
public void OnActionExecuting(ActionExecutingContext context) // before controller method run | ||
{ | ||
} | ||
} | ||
} |
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,23 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ParkingLotApi.Exceptions; | ||
|
||
namespace ParkingLotApi.Filter | ||
{ | ||
public class InvalidNameExceptionFilter : IActionFilter, IOrderedFilter | ||
{ | ||
public int Order => int.MaxValue - 10; | ||
public void OnActionExecuted(ActionExecutedContext context) // when controller method return | ||
{ | ||
if (context.Exception is InvalidNameException) | ||
{ | ||
context.Result = new BadRequestResult(); | ||
context.ExceptionHandled = true; | ||
} | ||
} | ||
|
||
public void OnActionExecuting(ActionExecutingContext context) // before controller method run | ||
{ | ||
} | ||
} | ||
} |
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,20 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
|
||
namespace ParkingLotApi.Models | ||
{ | ||
public class ParkingLot | ||
{ | ||
public 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 ParkingLotDatabaseSetting | ||
{ | ||
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 |
---|---|---|
@@ -1,11 +1,44 @@ | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using ParkingLotApi.Filter; | ||
using ParkingLotApi.Models; | ||
using ParkingLotApi.Reposirities; | ||
using ParkingLotApi.Services; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
builder.Services.AddControllers(options => | ||
{ | ||
options.Filters.Add<InvalidCapacityExceptionFilter>(); | ||
}); | ||
|
||
builder.Services.AddControllers(options => | ||
{ | ||
options.Filters.Add<InvalidIdExceptionFilter>(); | ||
}); | ||
|
||
builder.Services.AddControllers(options => | ||
{ | ||
options.Filters.Add<InvalidNameExceptionFilter>(); | ||
}); | ||
|
||
builder.Services.AddControllers().AddNewtonsoftJson(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
builder.Services.AddScoped<ParkingLotService>(); | ||
builder.Services.AddSingleton<IParkingLotRepository, ParkingLotRepository>(); | ||
|
||
builder.Services.Configure<ParkingLotDatabaseSetting>( | ||
builder.Configuration.GetSection("ParkingLotDatabase")); | ||
|
||
builder.Services.AddControllers(options => | ||
{ | ||
options.InputFormatters.Insert(0, MyJPIF.GetJsonPatchInputFormatter()); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
|
@@ -22,4 +55,25 @@ | |
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
app.Run(); | ||
|
||
public partial class Program { } | ||
|
||
public static class MyJPIF | ||
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. 介绍一下如何使用 |
||
{ | ||
public static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter() | ||
{ | ||
var builder = new ServiceCollection() | ||
.AddLogging() | ||
.AddMvc() | ||
.AddNewtonsoftJson() | ||
.Services.BuildServiceProvider(); | ||
|
||
return builder | ||
.GetRequiredService<IOptions<MvcOptions>>() | ||
.Value | ||
.InputFormatters | ||
.OfType<NewtonsoftJsonPatchInputFormatter>() | ||
.First(); | ||
} | ||
} |
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,15 @@ | ||
using ParkingLotApi.Dtos; | ||
using ParkingLotApi.Models; | ||
|
||
namespace ParkingLotApi.Reposirities | ||
{ | ||
public interface IParkingLotRepository | ||
{ | ||
public Task<ParkingLot> CreateParkingLot(ParkingLot parkingLot); | ||
public Task DeleteParkingLot(string id); | ||
public Task<List<ParkingLot>> GetParkingLotPartial(int pageSize, int pageIndex); | ||
public Task<ParkingLot> GetParkingLotById(string id); | ||
public Task<ParkingLot> UpdateParkingLot(string id, ParkingLot parkingLot); | ||
public Task<ParkingLot> GetParkingLotByName(string name); | ||
} | ||
} |
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.
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.
很好,将API文档单独编写,虽然没写全