Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 10 additions & 2 deletions NorthwindCRUD/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Microsoft.AspNetCore.Mvc;
using NorthwindCRUD.Models.DbModels;
using NorthwindCRUD.Models.Dtos;
using NorthwindCRUD.Models.Errors;
using NorthwindCRUD.Services;
using Swashbuckle.AspNetCore.Annotations;

[ApiController]
[Route("[controller]")]
Expand Down Expand Up @@ -76,7 +78,10 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
}

[HttpPost]
[Authorize]
[SwaggerResponse(400, "Your inputs do not pass validation!", typeof(Errors), "text/json")]
Copy link

Choose a reason for hiding this comment

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

if model is not valid this will return:

return BadRequest(ModelState);

which will end up to something like this:
image

What should return this end point in this case is Errors. What you need to do is something like this:

                if (ModelState.IsValid == false)
                {
                    // TODO: Validate each field here
                    if (model.ContactName != "Infragistics")
                    {
                        return BadRequest(new Errors
                        {
                            CustomErrors = new ValidationError[]
                            {
                                new ValidationError
                                {
                                    DataField = nameof(model.ContactName),
                                    Message = "Contact Name is not valid",
                                    StatusCode = 401
                                },
                            },
                        });
                    }
                }

                var mappedModel = this.mapper.Map<CustomerDto, CustomerDb>(model);
                var customer = this.customerService.Create(mappedModel);
                return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));

[SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")]

// [Authorize]
public ActionResult<CustomerDto> Create(CustomerDto model)
{
try
Expand Down Expand Up @@ -126,7 +131,10 @@ public ActionResult<CustomerDto> Update(CustomerDto model)
}

[HttpDelete("{id}")]
[Authorize]
[SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")]
[SwaggerResponse(404, "Your client is not found!", typeof(CustomError), "text/json")]

// [Authorize]
public ActionResult<CustomerDto> Delete(string id)
{
try
Expand Down
13 changes: 13 additions & 0 deletions NorthwindCRUD/Models/Errors/CustomError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NorthwindCRUD.Models.Errors
{
public class CustomError
{
public CustomError()
Copy link

Choose a reason for hiding this comment

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

no need of empty constructor.

{
}

public int StatusCode { get; set; }

public string Message { get; set; }
}
}
11 changes: 11 additions & 0 deletions NorthwindCRUD/Models/Errors/Errors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace NorthwindCRUD.Models.Errors
{
public class Errors
{
public Errors()
Copy link

Choose a reason for hiding this comment

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

no need of empty constructor.

{
}

public ValidationError[] CustomErrors { get; set; }
}
}
13 changes: 13 additions & 0 deletions NorthwindCRUD/Models/Errors/ValidationError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel;

namespace NorthwindCRUD.Models.Errors
{
public class ValidationError : CustomError
{
public ValidationError()
Copy link

Choose a reason for hiding this comment

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

no need of empty constructor.

{
}

public string DataField { get; set; }
}
}