Добавил добавление объекта
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 12s

This commit is contained in:
2026-07-03 18:35:09 +03:00
parent 5b14466d24
commit fd6506af7b
16 changed files with 336 additions and 54 deletions
+30 -12
View File
@@ -17,15 +17,16 @@ public class DeviceController : ControllerBase
}
//=========================== GET =================================//
#region GET
[HttpGet("GetTypes")]
[ProducesResponseType<List<DeviceType>>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult GetList()
public IActionResult GetList()// Получить лист типов устройств
{
using (var db = new SQLiteService())
{
var tps = db.DeviceTypes.ToList();
if (tps.Count != 0)
if (tps.Count != 0)
return Ok(tps);
else return NoContent();
}
@@ -34,7 +35,7 @@ public class DeviceController : ControllerBase
[HttpGet("GetDeviceById")]
[ProducesResponseType<DeviceItem>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult GetById(int id)
public IActionResult GetById(int id) // Получить устройство по id
{
using (var db = new SQLiteService())
{
@@ -48,20 +49,22 @@ public class DeviceController : ControllerBase
[HttpGet("GetDevices")]
[ProducesResponseType<DeviceItem>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult GetAll()
public IActionResult GetAll() // Получить все устройства
{
using (var db = new SQLiteService())
{
var d = db.Devices.ToList();
//if (d.Count != 0)
return Ok(d);
return Ok(d);
//else return NoContent();
}
}
}
#endregion
//========================= POST ====================================//
#region POST
[HttpPost("AddDevice")]
public IActionResult AddDevice(DeviceItem d)
public IActionResult AddDevice(DeviceItem d) // Добавить одно устройство
{
using (var db = new SQLiteService())
{
@@ -72,7 +75,7 @@ public class DeviceController : ControllerBase
}
[HttpPost("AddType")]
public IActionResult AddType(DeviceType d)
public IActionResult AddType(DeviceType d) //Добавить один тип устройства
{
using (var db = new SQLiteService())
{
@@ -80,18 +83,33 @@ public class DeviceController : ControllerBase
db.SaveChanges();
return Created();
}
}
}
#endregion
//========================= DELETE ====================================//
#region DELETE
[HttpDelete("DelType")]
public IActionResult DelType(int id)
public IActionResult DelType(int id) //Удалить тип устройства по id
{
using (var db = new SQLiteService())
{
var deldev = db.DeviceTypes.Where(u => u.Id == id).FirstOrDefault();
db.DeviceTypes.Remove(deldev);
var deldevtype = db.DeviceTypes.Where(u => u.Id == id).FirstOrDefault();
db.DeviceTypes.Remove(deldevtype);
db.SaveChanges();
return Ok();
}
}
[HttpDelete("DelDevice")]
public IActionResult DelDevice(int id) //Удалить устройство по id
{
using (var db = new SQLiteService())
{
var deldev = db.Devices.Where(u => u.Id == id).FirstOrDefault();
db.Devices.Remove(deldev);
db.SaveChanges();
return Ok();
}
}
#endregion
}
+86
View File
@@ -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();
}
}
}