Skip to content

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
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ParkingLotApi/Controllers/ApiDesign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
URL: parkinglots
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

很好,将API文档单独编写,虽然没写全


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"
}
"""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well: Api Doc included

65 changes: 65 additions & 0 deletions ParkingLotApi/Controllers/ParkingLotsController.cs
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);
}
}
}
}
33 changes: 0 additions & 33 deletions ParkingLotApi/Controllers/WeatherForecastController.cs

This file was deleted.

16 changes: 16 additions & 0 deletions ParkingLotApi/Dtos/ParkingLotDto.cs
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 };
}
}
}
6 changes: 6 additions & 0 deletions ParkingLotApi/Exceptions/InvalidCapacityException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ParkingLotApi.Exceptions
{
public class InvalidCapacityException : Exception
{
}
}
6 changes: 6 additions & 0 deletions ParkingLotApi/Exceptions/InvalidIdException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ParkingLotApi.Exceptions
{
public class InvalidIdException : Exception
{
}
}
6 changes: 6 additions & 0 deletions ParkingLotApi/Exceptions/InvalidNameException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ParkingLotApi.Exceptions
{
public class InvalidNameException : Exception
{
}
}
25 changes: 25 additions & 0 deletions ParkingLotApi/Filter/InvalidCapacityExceptionFilter.cs
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
{
}
}
}
24 changes: 24 additions & 0 deletions ParkingLotApi/Filter/InvalidIdExceptionFilter.cs
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
{
}
}
}
23 changes: 23 additions & 0 deletions ParkingLotApi/Filter/InvalidNameExceptionFilter.cs
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
{
}
}
}
20 changes: 20 additions & 0 deletions ParkingLotApi/Models/ParkingLot.cs
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; }
}
}
9 changes: 9 additions & 0 deletions ParkingLotApi/Models/ParkingLotDatabaseSetting.cs
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!;
}
}
3 changes: 3 additions & 0 deletions ParkingLotApi/ParkingLotApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageReference Include="MongoDB.Bson" Version="2.22.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
58 changes: 56 additions & 2 deletions ParkingLotApi/Program.cs
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();

Expand All @@ -22,4 +55,25 @@

app.MapControllers();

app.Run();
app.Run();

public partial class Program { }

public static class MyJPIF
Copy link

Choose a reason for hiding this comment

The 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();
}
}
15 changes: 15 additions & 0 deletions ParkingLotApi/Reposirities/IParkingLotRepository.cs
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);
}
}
Loading