source: WineTrackerFinal/WineTrackerWebApi/Controllers/VehicleTypeController.cs

main
Last change on this file was 17d6948, checked in by Nikola Mishevski <Nikola.Mishevski@…>, 6 days ago

initial commit WineTracker Project

  • Property mode set to 100644
File size: 6.5 KB
Line 
1using System.Data;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using Npgsql;
5using Dapper;
6using WineTrackerWebApi.Models.VehicleType;
7using WineTrackerWebApi.Models.WineType;
8using WineTrackerWebApi.Models.ErrorHandle;
9using WineTrackerWebApi.Helpers;
10
11namespace WineTrackerWebApi.Controllers
12{
13 [Route("api/[controller]")]
14 [ApiController]
15 public class VehicleTypeController : ControllerBase
16 {
17 private readonly string _connectionString;
18
19 public VehicleTypeController(IConfiguration configuration)
20 {
21 _connectionString = configuration.GetConnectionString("DefaultConnection");
22 }
23
24 // GET: api/VehicleType
25 [HttpGet]
26 public async Task<ActionResult<IEnumerable<VehicleType>>> GetVehicleType()
27 {
28 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
29 {
30 const string query = $@"select vehicle_type_id as VehicleTypeId, vehicle_type_name as VehicleTypeName from vehicle_type vt";
31 var vehicleTypes = await db.QueryAsync<VehicleType>(query);
32 return Ok(vehicleTypes);
33 }
34 }
35
36 // GET: api/VehicleType/5
37 [HttpGet("{id}")]
38 public async Task<ActionResult<WineType>> GetVehicleType(int id)
39 {
40 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
41 {
42 var parameters = new { id = id };
43
44 string query = $@"select vehicle_type_id as VehicleTypeId, vehicle_type_name as VehicleTypeName from vehicle_type vt where vehicle_type_id = @id";
45 var vehicleType = await db.QueryFirstAsync<VehicleType>(query, parameters);
46 return Ok(vehicleType);
47 }
48 }
49
50 // PUT: api/VehicleType/5
51 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
52 [HttpPut]
53 public async Task<IActionResult> PutVehicleType([FromBody] VehicleType vehicleType)
54 {
55 try
56 {
57 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
58 {
59 db.Open();
60 using (var transaction = db.BeginTransaction()) // Start a transaction
61 {
62 var parameters = new { VehicleTypeName = vehicleType.VehicleTypeName.Trim(), VehicleTypeId = vehicleType.VehicleTypeId };
63
64 await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Vehicle_Type", parameters.VehicleTypeName, entityId: parameters.VehicleTypeId);
65
66 string query = $@"update vehicle_type set vehicle_type_name = @VehicleTypeName where vehicle_type_id = @VehicleTypeId";
67 await db.ExecuteAsync(query, parameters, transaction: transaction);
68
69 transaction.Commit();
70 return Ok();
71 }
72 }
73 }
74 catch (Exception ex)
75 {
76 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
77 }
78 }
79
80 // POST: api/VehicleType
81 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
82 [HttpPost]
83 public async Task<IActionResult> PostVehicleType([FromBody] VehicleType vehicleType)
84 {
85 try
86 {
87 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
88 {
89 db.Open();
90 using (var transaction = db.BeginTransaction()) // Start a transaction
91 {
92 var parameters = new { VehicleTypeName = vehicleType.VehicleTypeName.Trim() };
93
94 await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Vehicle_Type", parameters.VehicleTypeName);
95
96 string query = "INSERT INTO vehicle_type (vehicle_type_name) VALUES (@VehicleTypeName);";
97 await db.ExecuteAsync(query, parameters, transaction: transaction);
98
99 transaction.Commit();
100 return Ok();
101 }
102 }
103 }
104 catch (Exception ex)
105 {
106 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
107 }
108 }
109
110 // DELETE: api/VehicleType/5
111 [HttpDelete("{id}")]
112 public async Task<IActionResult> DeleteVehicleType(int id)
113 {
114 try
115 {
116 //Check if there are vehicle details linked to the vehicle type we are trying to delete, if so return a message and dont let the user delete the vehicle type.
117 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
118 {
119 db.Open();
120 using (var transaction = db.BeginTransaction()) // Start a transaction
121 {
122 var parameters = new { id = id };
123
124 string vehicleTypeLinkToVehicleDetailsQuery = $@"
125 select count(*)
126 from vehicle_type vt
127 join vehicle_details vd on vd.vehicle_type_id = vt.vehicle_type_id
128 where vt.vehicle_type_id = @id";
129
130 var isVehicleTypeLinkedToVehicleDetails = await db.QueryFirstAsync<int>(vehicleTypeLinkToVehicleDetailsQuery, parameters, transaction: transaction);
131 if (isVehicleTypeLinkedToVehicleDetails > 0)
132 return BadRequest(new ErrorHandler { Name = "Vehicle Type Cannot Be Deleted", Description = "Vehicle Type cannot be deleted because it is linked to vehicle details" });
133
134 string query = $@"delete from vehicle_type where vehicle_type_id = @id";
135 await db.ExecuteAsync(query, parameters, transaction: transaction);
136
137 transaction.Commit();
138 return Ok();
139 }
140 }
141 }
142 catch (Exception ex)
143 {
144 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
145 }
146 }
147 }
148}
Note: See TracBrowser for help on using the repository browser.