Skip to content

Commit f64ea7f

Browse files
committed
[#1] [add] more different controllers for test
[#1] [add] names formatting [#1] [del] unused
1 parent 90e8535 commit f64ea7f

13 files changed

Lines changed: 229 additions & 68 deletions

src/Simplify.Web.Swagger/SimplifyWebDocumentFilter.cs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ namespace Simplify.Web.Swagger
1111
/// </summary>
1212
public class SimplifyWebDocumentFilter : IDocumentFilter
1313
{
14-
private const string VersionEndPoint = "/version";
14+
// private const string VersionEndPoint = "/version";
15+
16+
private static IList<string> RemovePrefixes = new List<string>
17+
{
18+
"Controllers.",
19+
"Api.v1."
20+
};
1521

1622
/// <summary>
1723
/// Applies current filter
@@ -47,18 +53,23 @@ private static OpenApiPathItem CreateOpenApiPathItems(HttpMethod method, string
4753
{
4854
var pathItem = new OpenApiPathItem();
4955

50-
pathItem.AddOperation(HttpMethodToOperationType(method), CreateOperation());
56+
pathItem.AddOperation(HttpMethodToOperationType(method), CreateOperation(item));
5157

5258
return pathItem;
5359
}
5460

55-
private static OpenApiOperation CreateOperation()
61+
private static OpenApiOperation CreateOperation(IControllerMetaData item)
5662
{
63+
// TODO
64+
5765
var operation = new OpenApiOperation
5866
{
5967
};
6068

61-
// operation.Responses.Add("200", response);
69+
operation.Tags.Add(new OpenApiTag
70+
{
71+
Name = FormatOperationName(item)
72+
});
6273

6374
return operation;
6475
}
@@ -75,17 +86,40 @@ private static OperationType HttpMethodToOperationType(HttpMethod method) =>
7586
_ => OperationType.Get
7687
};
7788

78-
// private KeyValuePair<string, OpenApiResponse> CreateResponse()
79-
// {
80-
// var response = new OpenApiResponse
81-
// {
82-
// Description = "Success"
83-
// };
89+
// TODO
90+
private static KeyValuePair<string, OpenApiResponse> CreateResponse()
91+
{
92+
var response = new OpenApiResponse
93+
{
94+
};
8495

85-
// return response;
86-
// }
96+
return new KeyValuePair<string, OpenApiResponse>("", response);
97+
}
8798

8899
private static string FormatControllerName(string route, HttpMethod method, bool needToAddPostfix) =>
89100
needToAddPostfix ? $"{route} ({method})" : route;
101+
102+
private static string? FormatOperationName(IControllerMetaData item) =>
103+
item.ControllerType.FullName != null ? FormatOperationName(item.ControllerType.FullName) : null;
104+
105+
private static string FormatOperationName(string str)
106+
{
107+
foreach (var prefix in RemovePrefixes)
108+
{
109+
var prefixIndex = str.IndexOf(prefix);
110+
111+
if (prefixIndex == -1)
112+
continue;
113+
114+
str = str.Substring(prefixIndex + prefix.Length);
115+
}
116+
117+
str = str.Replace(".", "/");
118+
119+
if (str.EndsWith("Controller"))
120+
str = str.Substring(0, str.LastIndexOf("Controller"));
121+
122+
return str;
123+
}
90124
}
91125
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Simplify.Web;
3+
using Simplify.Web.Attributes;
4+
using Simplify.Web.Json.Responses;
5+
using TesterApp.ViewModels;
6+
7+
namespace TesterApp.Controllers.Api.v1.Groups;
8+
9+
[Get("/api/v1/groups")]
10+
[ApiVersion("1.0")]
11+
[Produces("application/json")]
12+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IList<GroupViewModel>))]
13+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
14+
public class GetMultipleController : Simplify.Web.Controller
15+
{
16+
public override ControllerResponse Invoke()
17+
{
18+
var items = new List<GroupViewModel>
19+
{
20+
new GroupViewModel
21+
{
22+
Name = "Group 1"
23+
},
24+
new GroupViewModel
25+
{
26+
Name = "Group 2"
27+
}
28+
};
29+
30+
// Items retrieve
31+
32+
return new Json(items);
33+
}
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Simplify.Web;
3+
using Simplify.Web.Attributes;
4+
5+
namespace TesterApp.Controllers.Api.v1.Groups;
6+
7+
[Patch("/api/v1/groups/{id:int}/rename")]
8+
[Authorize]
9+
[ApiVersion("1.0")]
10+
[Produces("application/text")]
11+
[ProducesResponseType(StatusCodes.Status204NoContent)]
12+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
13+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
14+
public class RenameController : Simplify.Web.Controller
15+
{
16+
public override ControllerResponse Invoke()
17+
{
18+
if (RouteParameters.id <= 0)
19+
return StatusCode(400, "User ID is invalid");
20+
21+
return NoContent();
22+
}
23+
}

src/TesterApp/Controllers/Api/v1/TestInController.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/TesterApp/Controllers/Api/v1/TestOutController.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Simplify.Web;
3+
using Simplify.Web.Attributes;
4+
using TesterApp.ViewModels;
5+
6+
namespace TesterApp.Controllers.Api.v1.Users;
7+
8+
[Post("/api/v1/users")]
9+
[ApiVersion("1.0")]
10+
[Produces("application/text")]
11+
public class CreateController : AsyncController<UserAddViewModel>
12+
{
13+
public override async Task<ControllerResponse> Invoke()
14+
{
15+
await ReadModelAsync();
16+
17+
return Content($"User created at {DateTime.Now.ToLongTimeString()}'");
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Simplify.Web;
3+
using Simplify.Web.Attributes;
4+
5+
namespace TesterApp.Controllers.Api.v1.Users;
6+
7+
[Delete("/api/v1/users/{id:int}")]
8+
[Authorize]
9+
[ApiVersion("1.0")]
10+
[Produces("application/text")]
11+
[ProducesResponseType(StatusCodes.Status204NoContent)]
12+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
13+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
14+
public class DeleteController : Simplify.Web.Controller
15+
{
16+
public override ControllerResponse Invoke()
17+
{
18+
if (RouteParameters.id <= 0)
19+
return StatusCode(400, "User ID is invalid");
20+
21+
return NoContent();
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Simplify.Web;
3+
using Simplify.Web.Attributes;
4+
using Simplify.Web.Json.Responses;
5+
using TesterApp.ViewModels;
6+
7+
namespace TesterApp.Controllers.Api.v1.Users;
8+
9+
[Get("/api/v1/users/{id:int}")]
10+
[ApiVersion("1.0")]
11+
[Produces("application/json")]
12+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserViewModel))]
13+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
14+
public class GetController : Simplify.Web.Controller
15+
{
16+
public override ControllerResponse Invoke() =>
17+
new Json(new UserViewModel
18+
{
19+
UserName = "User 1",
20+
CreationTime = DateTime.Now
21+
});
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Simplify.Web;
3+
using Simplify.Web.Attributes;
4+
using Simplify.Web.Json.Responses;
5+
using TesterApp.ViewModels;
6+
7+
namespace TesterApp.Controllers.Api.v1.Users;
8+
9+
[Get("/api/v1/users")]
10+
[ApiVersion("1.0")]
11+
[Produces("application/json")]
12+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IList<UserViewModel>))]
13+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
14+
public class GetMultipleController : Simplify.Web.Controller
15+
{
16+
public override ControllerResponse Invoke()
17+
{
18+
var items = new List<UserViewModel>
19+
{
20+
new UserViewModel
21+
{
22+
UserName = "User 1",
23+
CreationTime = DateTime.Now
24+
},
25+
new UserViewModel
26+
{
27+
UserName = "User 2",
28+
CreationTime = DateTime.Now.Subtract(TimeSpan.FromDays(1))
29+
}
30+
};
31+
32+
return new Json(items);
33+
}
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TesterApp.ViewModels
2+
{
3+
public class GroupViewModel
4+
{
5+
public string Name { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)