Skip to content

Commit b6f3bff

Browse files
committed
API Analyzer added
API method, response types added - for better swagger documentation
1 parent 39edcd8 commit b6f3bff

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

Supermarket/V1/Controllers/AboutController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.Logging;
4+
using System.Threading.Tasks;
35

46
namespace Supermarket.V1.Controller
57
{
@@ -16,6 +18,7 @@ public AboutController(ILogger<AboutController> logger)
1618
}
1719

1820
[HttpGet]
21+
[ProducesResponseType(StatusCodes.Status200OK)]
1922
public IActionResult Get()
2023
{
2124
_logger.LogInformation("Still consuming deprecated api method");

Supermarket/V1/Controllers/AccountController.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using AutoMapper;
22
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Identity;
45
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.Extensions.Options;
67
using Microsoft.IdentityModel.Tokens;
7-
using Supermarket.Extensions;
8-
using Supermarket.Identity.Models;
8+
using Supermarket.Core.Models;
99
using Supermarket.Resources;
1010
using System;
1111
using System.IdentityModel.Tokens.Jwt;
@@ -37,11 +37,10 @@ public AccountController(UserManager<ApplicationUser> userManager, SignInManager
3737

3838
[HttpPost]
3939
[Route("Register")]
40+
[ProducesResponseType(StatusCodes.Status200OK)]
41+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
4042
public async Task<IActionResult> Register([FromBody]RegisterResource resource)
4143
{
42-
if (!ModelState.IsValid)
43-
return BadRequest(ModelState.GetErrorMessage());
44-
4544
var user = new ApplicationUser()
4645
{
4746
UserName = resource.Email,
@@ -56,11 +55,10 @@ public async Task<IActionResult> Register([FromBody]RegisterResource resource)
5655

5756
[HttpPost]
5857
[Route("Login")]
58+
[ProducesResponseType(StatusCodes.Status200OK)]
59+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
5960
public async Task<IActionResult> Login([FromBody]LoginResource resource)
6061
{
61-
if (!ModelState.IsValid)
62-
return BadRequest(ModelState.GetErrorMessage());
63-
6462
var user = await _userManager.FindByNameAsync(resource.Email);
6563

6664
if (user != null && await _userManager.CheckPasswordAsync(user, resource.Password))
@@ -91,6 +89,7 @@ public async Task<IActionResult> Login([FromBody]LoginResource resource)
9189
[HttpGet]
9290
[Authorize]
9391
[Route("Profile")]
92+
[ProducesResponseType(StatusCodes.Status200OK)]
9493
public async Task<IActionResult> GetUserInfo()
9594
{
9695
var userId = User.Claims.First(c => c.Type == "UserId").Value;

Supermarket/V1/Controllers/CategoriesController.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using AutoMapper;
22
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Mvc;
5+
using Supermarket.Core.Models;
46
using Supermarket.Domain.Services.Contracts;
5-
using Supermarket.Entites.Models;
6-
using Supermarket.Extensions;
77
using Supermarket.Resources;
88
using System.Collections.Generic;
99
using System.Threading.Tasks;
@@ -26,6 +26,7 @@ public CategoriesController(IServiceWrapper serviceWrapper, IMapper mapper)
2626
}
2727

2828
[HttpGet]
29+
[ProducesResponseType(StatusCodes.Status200OK)]
2930
public async Task<IActionResult> GetAllAsync()
3031
{
3132
var categories = await _serviceWrapper.Category.ListAsync();
@@ -36,11 +37,10 @@ public async Task<IActionResult> GetAllAsync()
3637
}
3738

3839
[HttpPost]
40+
[ProducesResponseType(StatusCodes.Status200OK)]
41+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
3942
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource)
4043
{
41-
if (!ModelState.IsValid)
42-
return BadRequest(ModelState.GetErrorMessage());
43-
4444
var category = _mapper.Map<SaveCategoryResource, Category>(resource);
4545
var result = await _serviceWrapper.Category.SaveAsync(category);
4646

@@ -52,11 +52,10 @@ public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resou
5252
}
5353

5454
[HttpPut("{id}")]
55+
[ProducesResponseType(StatusCodes.Status200OK)]
56+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
5557
public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryResource resource)
5658
{
57-
if (!ModelState.IsValid)
58-
return BadRequest(ModelState.GetErrorMessage());
59-
6059
var category = _mapper.Map<SaveCategoryResource, Category>(resource);
6160
var result = await _serviceWrapper.Category.UpdateAsync(id, category);
6261

@@ -68,6 +67,8 @@ public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryResourc
6867
}
6968

7069
[HttpDelete("{id}")]
70+
[ProducesResponseType(StatusCodes.Status200OK)]
71+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
7172
public async Task<IActionResult> DeleteAsync(int id)
7273
{
7374
var result = await _serviceWrapper.Category.DeleteAsync(id);

Supermarket/V1/Controllers/ProductsController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using AutoMapper;
22
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Mvc;
5+
using Supermarket.Core.Models;
46
using Supermarket.Domain.Services.Contracts;
5-
using Supermarket.Entites.Models;
67
using Supermarket.Resources;
78
using System.Collections.Generic;
89
using System.Threading.Tasks;
@@ -25,6 +26,7 @@ public ProductsController(IServiceWrapper serviceWrapper, IMapper mapper)
2526
}
2627

2728
[HttpGet]
29+
[ProducesResponseType(StatusCodes.Status200OK)]
2830
public async Task<IActionResult> GetAllAsync()
2931
{
3032
var products = await _serviceWrapper.Product.ListAsync();

Supermarket/V2/Controllers/AboutController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
23

34
namespace Supermarket.V2.Controller
45
{
@@ -8,6 +9,7 @@ namespace Supermarket.V2.Controller
89
public class AboutController : ControllerBase
910
{
1011
[HttpGet]
12+
[ProducesResponseType(StatusCodes.Status200OK)]
1113
public IActionResult Get()
1214
{
1315
return Ok(new { message = "This is about Api Version 2.0" });

0 commit comments

Comments
 (0)