113 lines
3.4 KiB
C#
113 lines
3.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using RDBMS_lab4.Models;
|
|
using RDBMS_lab4.ViewModels;
|
|
using System.Xml.Linq;
|
|
|
|
namespace RDBMS_lab4.Controllers
|
|
{
|
|
public class ServicesController : Controller
|
|
{
|
|
public ServicesController()
|
|
{
|
|
}
|
|
|
|
[HttpGet("get")]
|
|
public IActionResult get()
|
|
{
|
|
try
|
|
{
|
|
using var context = new beautySalonContext();
|
|
var res = context.Services.Select(b => new ServiceViue(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.Services.FirstOrDefault(b => b.Id == Id);
|
|
if (res == null)
|
|
{
|
|
return Ok(null);
|
|
}
|
|
return Ok(new ServiceViue(res));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
[HttpPost("create")]
|
|
public IActionResult create([FromBody] ServiceViue service)
|
|
{
|
|
try
|
|
{
|
|
using var context = new beautySalonContext();
|
|
var res = context.Services.Add(new()
|
|
{
|
|
Name = service.Name,
|
|
Price = (decimal)service.Price,
|
|
Duration = new TimeSpan(service.Duration)
|
|
});
|
|
context.SaveChanges();
|
|
if (res == null)
|
|
{
|
|
return BadRequest(new NullReferenceException());
|
|
}
|
|
return Ok(new ServiceViue(res.Entity));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
[HttpPut("update")]
|
|
public IActionResult update([FromBody] ServiceViue service)
|
|
{
|
|
try
|
|
{
|
|
using var context = new beautySalonContext();
|
|
var res = context.Services.FirstOrDefault(b => b.Id == service.Id);
|
|
res.Name = service.Name;
|
|
res.Price = (decimal)service.Price;
|
|
res.Duration = new TimeSpan(service.Duration);
|
|
context.SaveChanges();
|
|
return Ok(new ServiceViue(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.Services.FirstOrDefault(b => b.Id == Id);
|
|
if (res != null)
|
|
{
|
|
context.Services.Remove(res);
|
|
context.SaveChanges();
|
|
}
|
|
if (res == null)
|
|
{
|
|
return BadRequest(new NullReferenceException());
|
|
}
|
|
return Ok(new ServiceViue(res));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|