This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Strela.Models;
|
||||
using Strela.Services;
|
||||
|
||||
namespace Strela.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ObjectController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<ObjectController> _logger;
|
||||
|
||||
public ObjectController(ILogger<ObjectController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
//=========================== GET =================================//
|
||||
[HttpGet("GetObject")]
|
||||
[ProducesResponseType<List<ObjectItem>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public IActionResult GetList()
|
||||
{
|
||||
using (var db = new SQLiteService())
|
||||
{
|
||||
var oi = db.ObjectItems.ToList();
|
||||
//if (oi.Count != 0)
|
||||
return Ok(oi);
|
||||
//else return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("GetObjectById")]
|
||||
[ProducesResponseType<ObjectItem>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public IActionResult GetById(int id)
|
||||
{
|
||||
using (var db = new SQLiteService())
|
||||
{
|
||||
var o = db.ObjectItems.Where(o => o.Id == id).FirstOrDefault();
|
||||
if (o != null)
|
||||
return Ok(o);
|
||||
else return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("GetObjectByName")]
|
||||
[ProducesResponseType<ObjectItem>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public IActionResult GetByName(string name)
|
||||
{
|
||||
using (var db = new SQLiteService())
|
||||
{
|
||||
var o = db.ObjectItems.Where(ob => ob.NameObject == name).FirstOrDefault();
|
||||
if (o != null)
|
||||
return Ok(o);
|
||||
else return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//========================= POST ====================================//
|
||||
[HttpPost("AddObject")]
|
||||
public IActionResult AddObject(ObjectItem o)
|
||||
{
|
||||
using (var db = new SQLiteService())
|
||||
{
|
||||
db.ObjectItems.Add(o);//.Where(u => u.Name == name).FirstOrDefault();
|
||||
db.SaveChanges();
|
||||
return Created();
|
||||
}
|
||||
}
|
||||
|
||||
//========================= DELETE ====================================//
|
||||
[HttpDelete("DelObject")]
|
||||
public IActionResult DelObject(int id)
|
||||
{
|
||||
using (var db = new SQLiteService())
|
||||
{
|
||||
var delobj = db.ObjectItems.Where(o => o.Id == id).FirstOrDefault();
|
||||
db.ObjectItems.Remove(delobj);
|
||||
db.SaveChanges();
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user