[17d6948] | 1 | using System.Data;
|
---|
| 2 | using Dapper;
|
---|
| 3 | using Microsoft.AspNetCore.Mvc;
|
---|
| 4 | using Microsoft.EntityFrameworkCore;
|
---|
| 5 | using Npgsql;
|
---|
| 6 | using WineTrackerWebApi.Helpers;
|
---|
| 7 | using WineTrackerWebApi.Models.ErrorHandle;
|
---|
| 8 | using WineTrackerWebApi.Models.Vehicle;
|
---|
| 9 |
|
---|
| 10 | namespace WineTrackerWebApi.Controllers
|
---|
| 11 | {
|
---|
| 12 | [Route("api/[controller]")]
|
---|
| 13 | [ApiController]
|
---|
| 14 | public class VehicleController : ControllerBase
|
---|
| 15 | {
|
---|
| 16 | private readonly string _connectionString;
|
---|
| 17 |
|
---|
| 18 | public VehicleController(IConfiguration configuration)
|
---|
| 19 | {
|
---|
| 20 | _connectionString = configuration.GetConnectionString("DefaultConnection");
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | // GET: api/Vehicle
|
---|
| 24 | [HttpGet]
|
---|
| 25 | public async Task<ActionResult<IEnumerable<VehicleDetails>>> GetVehicle()
|
---|
| 26 | {
|
---|
| 27 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 28 | {
|
---|
| 29 | const string query = $@"
|
---|
| 30 | select
|
---|
| 31 | v.vehicle_id as VehicleId,
|
---|
| 32 | v.registration as Registration,
|
---|
| 33 | CONCAT('Type: ', vt.vehicle_type_name ,' | Make: ', vd.make, ' | Model: ', vd.model, ' | Capacity: ', vd.capacity) as Details
|
---|
| 34 | from vehicle v
|
---|
| 35 | join vehicle_details vd on v.vehicle_details_id = vd.vehicle_details_id
|
---|
| 36 | join vehicle_type vt on vd.vehicle_type_id = vt.vehicle_type_id
|
---|
| 37 | ";
|
---|
| 38 | var vehicleDetails = await db.QueryAsync<VehicleDetails>(query);
|
---|
| 39 | return Ok(vehicleDetails);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | // GET: api/Vehicle/5
|
---|
| 44 | [HttpGet("{id}")]
|
---|
| 45 | public async Task<ActionResult<Vehicle>> GetVehicle(int id)
|
---|
| 46 | {
|
---|
| 47 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 48 | {
|
---|
| 49 | var parameters = new { id = id };
|
---|
| 50 |
|
---|
| 51 | const string query = $@"
|
---|
| 52 | select
|
---|
| 53 | v.vehicle_id as VehicleId,
|
---|
| 54 | vd.vehicle_details_id as VehicleDetailsId,
|
---|
| 55 | v.registration as Registration
|
---|
| 56 | from vehicle v
|
---|
| 57 | join vehicle_details vd on v.vehicle_details_id = vd.vehicle_details_id
|
---|
| 58 | where v.vehicle_id = @id
|
---|
| 59 | ";
|
---|
| 60 | var vehicle = await db.QueryFirstAsync<Vehicle>(query, parameters);
|
---|
| 61 | return Ok(vehicle);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | // PUT: api/Vehicle/5
|
---|
| 66 | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
---|
| 67 | [HttpPut]
|
---|
| 68 | public async Task<IActionResult> PutVehicle([FromBody] Vehicle vehicle)
|
---|
| 69 | {
|
---|
| 70 | try
|
---|
| 71 | {
|
---|
| 72 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 73 | {
|
---|
| 74 | db.Open();
|
---|
| 75 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
| 76 | {
|
---|
| 77 | var parameters = new { Registration = vehicle.Registration.Trim(), VehicleDetailsId = vehicle.VehicleDetailsId, VehicleId = vehicle.VehicleId };
|
---|
| 78 |
|
---|
| 79 | await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Vehicle", parameters.Registration, "registration", entityId: parameters.VehicleId);
|
---|
| 80 |
|
---|
| 81 | string query = $@"
|
---|
| 82 | update vehicle
|
---|
| 83 | set
|
---|
| 84 | vehicle_details_id = @VehicleDetailsId,
|
---|
| 85 | registration = @Registration
|
---|
| 86 | where vehicle_id = @VehicleId
|
---|
| 87 | ";
|
---|
| 88 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
| 89 |
|
---|
| 90 | transaction.Commit();
|
---|
| 91 | return Ok();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | catch (Exception ex)
|
---|
| 96 | {
|
---|
| 97 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // POST: api/Vehicle
|
---|
| 102 | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
---|
| 103 | [HttpPost]
|
---|
| 104 | public async Task<IActionResult> PostVehicle([FromBody] Vehicle vehicle)
|
---|
| 105 | {
|
---|
| 106 | try
|
---|
| 107 | {
|
---|
| 108 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 109 | {
|
---|
| 110 | db.Open();
|
---|
| 111 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
| 112 | {
|
---|
| 113 | var parameters = new { Registration = vehicle.Registration.Trim(), VehicleDetailsId = vehicle.VehicleDetailsId };
|
---|
| 114 |
|
---|
| 115 | string query = $@"insert into vehicle (vehicle_details_id, registration) values (@VehicleDetailsId, @Registration)";
|
---|
| 116 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
| 117 |
|
---|
| 118 | transaction.Commit();
|
---|
| 119 | return Ok();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | catch (Exception ex)
|
---|
| 124 | {
|
---|
| 125 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | // DELETE: api/Vehicle/5
|
---|
| 130 | [HttpDelete("{id}")]
|
---|
| 131 | public async Task<IActionResult> DeleteVehicle(int id)
|
---|
| 132 | {
|
---|
| 133 | try
|
---|
| 134 | {
|
---|
| 135 | //Check if there is a shipment linked to the vehicle we are trying to delete, if so return a message and dont let the user delete the vehicle.
|
---|
| 136 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 137 | {
|
---|
| 138 | db.Open();
|
---|
| 139 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
| 140 | {
|
---|
| 141 | var parameters = new { id = id };
|
---|
| 142 |
|
---|
| 143 | string vehicleLinkToShipmentQuery = $@"
|
---|
| 144 | select Count(*)
|
---|
| 145 | from shipment sl
|
---|
| 146 | where vehicle_id = @id";
|
---|
| 147 |
|
---|
| 148 | var isVehicleLinkedToShipment = await db.QueryFirstAsync<int>(vehicleLinkToShipmentQuery, parameters, transaction: transaction);
|
---|
| 149 | if (isVehicleLinkedToShipment > 0)
|
---|
| 150 | return BadRequest(new ErrorHandler { Name = "Vehicle Cannot Be Deleted", Description = "Vehicle cannot be deleted because it is linked to a shipment" });
|
---|
| 151 |
|
---|
| 152 | string query = $@"delete from vehicle where vehicle_id = @id";
|
---|
| 153 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
| 154 |
|
---|
| 155 | transaction.Commit();
|
---|
| 156 | return Ok();
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | catch (Exception ex)
|
---|
| 161 | {
|
---|
| 162 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|