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