Skip to content

Instantly share code, notes, and snippets.

View nirzaf's full-sized avatar
🎯
Focusing

M.F.M Fazrin nirzaf

🎯
Focusing
View GitHub Profile
<div class="container">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</div>
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource)
{
if (!ModelState.IsValid)
return BadRequest(ModelState.GetErrorMessages());
var category = _mapper.Map<SaveCategoryResource, Category>(resource);
var result = await _categoryService.SaveAsync(category);
if (!result.Success)
using SuperMarketAPI.Domain.Services.Communication;
using SuperMarketAPI.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SuperMarketAPI.Domain.Services
{
public interface ICategoryService
{
Task<IEnumerable<Category>> ListAsync();
using SuperMarketAPI.Models;
namespace SuperMarketAPI.Domain.Services.Communication
{
public class SaveCategoryResponse : BaseResponse
{
public Category Category { get; private set; }
private SaveCategoryResponse(bool success, string message, Category category) : base(success, message)
{
namespace SuperMarketAPI.Domain.Services.Communication
{
public abstract class BaseResponse
{
public bool Success { get; protected set; }
public string Message { get; protected set; }
public BaseResponse(bool success, string message)
{
Success = success;
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource)
{
if (!ModelState.IsValid)
return BadRequest(ModelState.GetErrorMessages());
var category = _mapper.Map<SaveCategoryResource, Category>(resource);
}
using AutoMapper;
using SuperMarketAPI.Models;
using SuperMarketAPI.Resources;
namespace SuperMarketAPI.Mapping
{
public class ResourceToModelProfile : Profile
{
public ResourceToModelProfile()
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Collections.Generic;
using System.Linq;
namespace SuperMarketAPI.Extention
{
public static class ModelStateExtensions
{
public static List<string> GetErrorMessages(this ModelStateDictionary dictionary)
{
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] SaveCategoryResource resource)
{
if (!ModelState.IsValid)
return BadRequest(ModelState.GetErrorMessages());
}
using System.ComponentModel.DataAnnotations;
namespace SuperMarketAPI.Resources
{
public class SaveCategoryResource
{
[Required]
[MaxLength(30)]
public string Name { get; set; }
}