109 lines
3.2 KiB
C#
109 lines
3.2 KiB
C#
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using RDBMS_lab4.ViewModels;
|
|||
|
|
|||
|
namespace RDBMS_lab4.Controllers
|
|||
|
{
|
|||
|
[Route("api/[controller]")]
|
|||
|
public class BuyersController : Controller
|
|||
|
{
|
|||
|
public BuyersController()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
[HttpGet("get")]
|
|||
|
public IActionResult get()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using var context = new beautySalonContext();
|
|||
|
var res = context.Buyers.Select(b => new BuyerViue(b)).ToList();
|
|||
|
return Ok(res);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return BadRequest(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
[HttpGet("get/{Id}")]
|
|||
|
public IActionResult get([FromRoute] int Id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using var context = new beautySalonContext();
|
|||
|
var res = context.Buyers.FirstOrDefault(b => b.Id == Id);
|
|||
|
if (res == null)
|
|||
|
{
|
|||
|
return Ok(null);
|
|||
|
}
|
|||
|
return Ok(new BuyerViue(res));
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return BadRequest(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
[HttpPost("create")]
|
|||
|
public IActionResult create([FromBody] BuyerViue buyer)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using var context = new beautySalonContext();
|
|||
|
var res = context.Buyers.Add(new() {
|
|||
|
Name = buyer.Name,
|
|||
|
Telephon = buyer.Telephon
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
if (res == null)
|
|||
|
{
|
|||
|
return BadRequest(new NullReferenceException());
|
|||
|
}
|
|||
|
return Ok(new BuyerViue(res.Entity));
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return BadRequest(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
[HttpPut("update")]
|
|||
|
public IActionResult update([FromBody] BuyerViue buyer)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using var context = new beautySalonContext();
|
|||
|
var res = context.Buyers.FirstOrDefault(b => b.Id == buyer.Id);
|
|||
|
res.Telephon = buyer.Telephon;
|
|||
|
res.Name = buyer.Name;
|
|||
|
context.SaveChanges();
|
|||
|
return Ok(new BuyerViue(res));
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return BadRequest(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
[HttpDelete("delete/{Id}")]
|
|||
|
public IActionResult delete([FromRoute] int Id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
using var context = new beautySalonContext();
|
|||
|
var res = context.Buyers.FirstOrDefault(b => b.Id == Id);
|
|||
|
if (res != null)
|
|||
|
{
|
|||
|
context.Buyers.Remove(res);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
if (res == null)
|
|||
|
{
|
|||
|
return BadRequest( new NullReferenceException());
|
|||
|
}
|
|||
|
return Ok(new BuyerViue(res));
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return BadRequest(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|