using System.Text.Json.Serialization;
namespace Services.CouponAPI.Models.Dto
{
public class ResponseDto
{
[JsonPropertyName("data")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault | JsonIgnoreCondition.WhenWritingNull)]
public object? Result { get; set; }
[JsonPropertyName("success")]
public bool IsSuccess { get; set; } = true;
[JsonPropertyName("message")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault | JsonIgnoreCondition.WhenWritingNull)]
public string? Message { get; set; }
}
}
using Services.CouponAPI.Data;
using Services.CouponAPI.Models;
using Services.CouponAPI.Models.Dto;
using Microsoft.AspNetCore.Mvc;
namespace Services.CouponAPI.Controllers
{
[Route("api/coupon")]
[ApiController]
public class CouponAPIController : ControllerBase
{
private readonly AppDbContext _db;
private ResponseDto _response;
public CouponAPIController(AppDbContext db)
{
_db = db;
_response = new ResponseDto();
}
[HttpGet]
public ResponseDto Get()
{
try
{
IEnumerable<Coupon> objList = _db.Coupons.ToList();
_response.Result = objList;
}
catch (Exception ex)
{
_response.IsSuccess = false;
_response.Message = ex.Message;
}
return _response;
}
[HttpGet]
[Route("{id:int}")]
public ResponseDto Get(int id)
{
try
{
Coupon obj = _db.Coupons.First(u => u.CouponId == id);
_response.Result = obj;
}
catch (Exception ex)
{
_response.IsSuccess = false;
_response.Message = ex.Message;
}
return _response;
}
}
}