From 53287bb390ccbe139787721f65e3bba5da43f411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A8=81=E5=8B=9D=20=E5=BC=B5?= Date: Sun, 4 Feb 2024 19:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BE=9E=E5=A4=A7=E6=9A=AA?= =?UTF-8?q?=E5=8F=A3=E7=B4=80=E9=8C=84=E6=95=B8=E6=93=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yuntech_in_car_tableController.cs | 123 ++++++++++++++++++ .../Models/Yuntech_in_car_table.cs | 11 ++ WebApi_data_value/Program.cs | 4 +- WebApi_data_value/Services/SqlContext.cs | 4 + WebApi_data_value/WebApi_data_value.csproj | 1 + 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs create mode 100644 WebApi_data_value/Models/Yuntech_in_car_table.cs diff --git a/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs new file mode 100644 index 0000000..5c77873 --- /dev/null +++ b/WebApi_data_value/Controllers/Yuntech_in_car_tableController.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using WebApi_data_value.Models; +using WebApi_data_value.Services; +using WebApi_data_value.ViewModel; + +namespace WebApi_data_value.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class Yuntech_in_car_tableController : ControllerBase + { + private readonly SqlContext _context; + + public Yuntech_in_car_tableController(SqlContext context) + { + _context = context; + } + + // GET: api/Yuntech_in_car_table + [HttpGet] + public async Task>> Getyuntech_in_car_table() + { + var in_car_table = await (from c in _context.yuntech_in_car_table + orderby c.in_time descending + select new Yuntech_in_car_table + { + license_plate_number = c.license_plate_number, + in_time = c.in_time, + out_time = c.out_time, + location = c.location, + } + ).ToListAsync(); + + + return in_car_table; + } + + + #region 獲取進入的單獨地區的所有資料 + // GET: api/Yuntech_in_car_table/5 + [HttpGet("location-{id}")] + public async Task> GetYuntech_in_car_table(string id) + { + var in_car_table = await (from c in _context.yuntech_in_car_table + where c.location == id + orderby c.in_time descending + select new Yuntech_in_car_table + { + license_plate_number = c.license_plate_number, + in_time = c.in_time, + out_time = c.out_time, + location = c.location, + } + ).ToListAsync(); + + + return in_car_table; + } + #endregion + + #region 獲取進入的單獨地區的所有資料 + // GET: api/Yuntech_in_car_table/5 + [HttpGet("location-{id}-time-{time}")] + public async Task> One_data(string id, DateTime time) + { + var in_car_table = await (from c in _context.yuntech_in_car_table + where c.location == id + where c.in_time == time + select c).FirstAsync(); + + + return in_car_table; + } + #endregion + + + + + // POST: api/Yuntech_in_car_table + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 + [HttpPost] + public async Task> PostYuntech_in_car_table(Yuntech_in_car_table yuntech_in_car_table) + { + if (_context.yuntech_in_car_table == null) + { + return Problem("Entity set 'SqlContext.yuntech_in_car_table' is null."); + } + _context.yuntech_in_car_table.Add(yuntech_in_car_table); + try + { + yuntech_in_car_table.in_time = DateTime.Now; + yuntech_in_car_table.out_time = null; + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + if (Yuntech_in_car_tableExists(yuntech_in_car_table.in_time)) + { + return Conflict(); + } + else + { + throw; + } + } + + return CreatedAtAction("GetYuntech_in_car_table", new { id = yuntech_in_car_table.in_time }, yuntech_in_car_table); + } + + + + private bool Yuntech_in_car_tableExists(DateTime? id) + { + return (_context.yuntech_in_car_table?.Any(e => e.in_time == id)).GetValueOrDefault(); + } + } +} diff --git a/WebApi_data_value/Models/Yuntech_in_car_table.cs b/WebApi_data_value/Models/Yuntech_in_car_table.cs new file mode 100644 index 0000000..9847b6b --- /dev/null +++ b/WebApi_data_value/Models/Yuntech_in_car_table.cs @@ -0,0 +1,11 @@ +namespace WebApi_data_value.Models +{ + public class Yuntech_in_car_table + { + public string? license_plate_number { get; set; } + public string? car_img { get; set; } + public string? location { get; set; } + public DateTime? in_time { get; set; } + public DateTime? out_time { get; set; } + } +} diff --git a/WebApi_data_value/Program.cs b/WebApi_data_value/Program.cs index d8dc62b..c6e436a 100644 --- a/WebApi_data_value/Program.cs +++ b/WebApi_data_value/Program.cs @@ -17,8 +17,8 @@ builder.Services.AddCors(); builder.Services.AddControllers(); // 連線PostgreSQL資料庫 -//var connectionString = "Server=140.125.20.183;UserID=postgres;password=EL404el404;Database=postgres;port=5432;Search Path=public;CommandTimeout=1800"; -var connectionString = "Server=127.0.0.1;UserID=postgres;password=EL404el404;Database=postgres;port=5432;Search Path=public;CommandTimeout=1800"; +var connectionString = "Server=140.125.20.183;UserID=postgres;password=EL404el404;Database=postgres;port=5432;Search Path=public;CommandTimeout=1800"; +//var connectionString = "Server=127.0.0.1;UserID=postgres;password=EL404el404;Database=postgres;port=5432;Search Path=public;CommandTimeout=1800"; builder.Services.AddDbContext(opt => opt.UseNpgsql(connectionString)); diff --git a/WebApi_data_value/Services/SqlContext.cs b/WebApi_data_value/Services/SqlContext.cs index 8d3aca5..02367f4 100644 --- a/WebApi_data_value/Services/SqlContext.cs +++ b/WebApi_data_value/Services/SqlContext.cs @@ -46,6 +46,9 @@ namespace WebApi_data_value.Services // 演算法 public DbSet parking_space_algorithm { get; set; } = null!; + //大門口 + public DbSet yuntech_in_car_table { get; set; } = null!; + protected override void OnModelCreating(ModelBuilder builder) { @@ -71,6 +74,7 @@ namespace WebApi_data_value.Services builder.Entity().HasKey(o => new { o.algorithm_serial_num }); //Primary Key + builder.Entity().HasKey(o => new { o.in_time }); //Primary Key } diff --git a/WebApi_data_value/WebApi_data_value.csproj b/WebApi_data_value/WebApi_data_value.csproj index 5ccfac7..b858af5 100644 --- a/WebApi_data_value/WebApi_data_value.csproj +++ b/WebApi_data_value/WebApi_data_value.csproj @@ -22,6 +22,7 @@ + all