parking-webapi/WebApi_data_value/Controllers/Violation_car_tableController.cs
2024-02-01 13:48:38 +08:00

194 lines
7.2 KiB
C#

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 Violation_car_tableController : ControllerBase
{
private readonly SqlContext _context;
public Violation_car_tableController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Violation_car_table
[HttpGet]
public async Task<ActionResult<IEnumerable<Violation_car_table>>> Getviolation_car_table()
{
if (_context.violation_car_table == null)
{
return NotFound();
}
return await _context.violation_car_table.ToListAsync();
}
#endregion
#region
// GET: api/Violation_car_table/5
[HttpGet("violation_location_name-{id}")]
public async Task<IEnumerable<Single_violation_car_table>> GetViolation_car_table(string id)
{
var violation_car_table = await(from c in _context.violation_car_table
where c.violation_location_name == id
orderby c.create_data_time descending
select new Single_violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
}
).ToListAsync();
return violation_car_table;
}
#endregion
#region
[HttpGet("location_nam-{id}-time-{time}")]
public async Task<ActionResult<Violation_car_table>> violation_location_name(string id, DateTime time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select new Violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
car_start_img = c.car_start_img,
car_end_img = c.car_end_img,
}).FirstAsync();
return violation_car_table;
}
#endregion
#region
[HttpPut("location_nam-{id}-time-{time}")]
public async Task<IActionResult> PutViolation_car_table(string id, DateTime time,Violation_car_table violation_car_table)
{
var violation_car_table_1 = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select c).FirstAsync();
_context.violation_car_table.Remove(violation_car_table_1);
await _context.SaveChangesAsync();
_context.violation_car_table.Add(violation_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
/*
// PUT: api/Violation_car_table/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutViolation_car_table(string id, Violation_car_table violation_car_table)
{
if (id != violation_car_table.violation_location_name)
{
return BadRequest();
}
_context.Entry(violation_car_table).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Violation_car_tableExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
*/
#region
// POST: api/Violation_car_table
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Violation_car_table>> PostViolation_car_table(Violation_car_table violation_car_table)
{
if (_context.violation_car_table == null)
{
return Problem("Entity set 'SqlContext.violation_car_table' is null.");
}
violation_car_table.create_data_time= DateTime.Now;
_context.violation_car_table.Add(violation_car_table);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Violation_car_tableExists(violation_car_table.violation_location_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetViolation_car_table", new { id = violation_car_table.violation_location_name }, violation_car_table);
}
#endregion
#region
// DELETE: api/Violation_car_table/5
[HttpDelete("location_name-{id}-time-{time}")]
public async Task<IActionResult> DeleteViolation_car_table(string id, DateTime time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select c).FirstAsync();
_context.violation_car_table.Remove(violation_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
private bool Violation_car_tableExists(string id)
{
return (_context.violation_car_table?.Any(e => e.violation_location_name == id)).GetValueOrDefault();
}
}
}