Skip to content

Commit

Permalink
Merge pull request #3 from mattbarry/master
Browse files Browse the repository at this point in the history
Refactor BaseResponse to use generic resource type
  • Loading branch information
evgomes authored Aug 11, 2019
2 parents 20108fc + 7f4ab10 commit ac7464e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/Supermarket.API/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resou
return BadRequest(new ErrorResource(result.Message));
}

var categoryResource = _mapper.Map<Category, CategoryResource>(result.Category);
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Resource);
return Ok(categoryResource);
}

Expand All @@ -77,7 +77,7 @@ public async Task<IActionResult> PutAsync(int id, [FromBody] SaveCategoryResourc
return BadRequest(new ErrorResource(result.Message));
}

var categoryResource = _mapper.Map<Category, CategoryResource>(result.Category);
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Resource);
return Ok(categoryResource);
}

Expand All @@ -98,7 +98,7 @@ public async Task<IActionResult> DeleteAsync(int id)
return BadRequest(new ErrorResource(result.Message));
}

var categoryResource = _mapper.Map<Category, CategoryResource>(result.Category);
var categoryResource = _mapper.Map<Category, CategoryResource>(result.Resource);
return Ok(categoryResource);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Supermarket.API/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<IActionResult> PostAsync([FromBody] SaveProductResource resour
return BadRequest(new ErrorResource(result.Message));
}

var productResource = _mapper.Map<Product, ProductResource>(result.Product);
var productResource = _mapper.Map<Product, ProductResource>(result.Resource);
return Ok(productResource);
}

Expand All @@ -79,7 +79,7 @@ public async Task<IActionResult> PutAsync(int id, [FromBody] SaveProductResource
return BadRequest(new ErrorResource(result.Message));
}

var productResource = _mapper.Map<Product, ProductResource>(result.Product);
var productResource = _mapper.Map<Product, ProductResource>(result.Resource);
return Ok(productResource);
}

Expand All @@ -100,7 +100,7 @@ public async Task<IActionResult> DeleteAsync(int id)
return BadRequest(new ErrorResource(result.Message));
}

var categoryResource = _mapper.Map<Product, ProductResource>(result.Product);
var categoryResource = _mapper.Map<Product, ProductResource>(result.Resource);
return Ok(categoryResource);
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/Supermarket.API/Domain/Services/Communication/BaseResponse.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
namespace Supermarket.API.Domain.Services.Communication
{
public abstract class BaseResponse
public abstract class BaseResponse<T>
{
public bool Success { get; protected set; }
public string Message { get; protected set; }
public bool Success { get; private set; }
public string Message { get; private set; }
public T Resource { get; private set; }

public BaseResponse(bool success, string message)
protected BaseResponse(T resource)
{
Success = success;
Success = true;
Message = string.Empty;
Resource = resource;
}

protected BaseResponse(string message)
{
Success = false;
Message = message;
Resource = default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,22 @@

namespace Supermarket.API.Domain.Services.Communication
{
public class CategoryResponse : BaseResponse
public class CategoryResponse : BaseResponse<Category>
{
public Category Category { get; private set; }

private CategoryResponse(bool success, string message, Category category) : base(success, message)
{
Category = category;
}

/// <summary>
/// Creates a success response.
/// </summary>
/// <param name="category">Saved category.</param>
/// <returns>Response.</returns>
public CategoryResponse(Category category) : this(true, string.Empty, category)
public CategoryResponse(Category category) : base(category)
{ }

/// <summary>
/// Creates am error response.
/// </summary>
/// <param name="message">Error message.</param>
/// <returns>Response.</returns>
public CategoryResponse(string message) : this(false, message, null)
public CategoryResponse(string message) : base(message)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

namespace Supermarket.API.Domain.Services.Communication
{
public class ProductResponse : BaseResponse
public class ProductResponse : BaseResponse<Product>
{
public Product Product { get; private set; }
public ProductResponse(Product product) : base(product) { }

private ProductResponse(bool success, string message, Product product) : base(success, message)
{
Product = product;
}

public ProductResponse(Product product) : this(true, string.Empty, product) { }

public ProductResponse(string message) : this(false, message, null) { }
public ProductResponse(string message) : base(message) { }
}
}

0 comments on commit ac7464e

Please sign in to comment.