source: WineTrackerFinal/WineTrackerWebApi/Controllers/WineTypeController.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: 7.2 KB
Line 
1using System.Data;
2using Dapper;
3using Microsoft.AspNetCore.Mvc;
4using Microsoft.EntityFrameworkCore;
5using Npgsql;
6using WineTrackerWebApi.Helpers;
7using WineTrackerWebApi.Models.ErrorHandle;
8using WineTrackerWebApi.Models.WineType;
9
10namespace WineTrackerWebApi.Controllers
11{
12 [Route("api/[controller]")]
13 [ApiController]
14 public class WineTypeController : ControllerBase
15 {
16 private readonly string _connectionString;
17
18 public WineTypeController(IConfiguration configuration)
19 {
20 _connectionString = configuration.GetConnectionString("DefaultConnection");
21 }
22
23 // GET: api/WineType
24 [HttpGet]
25 public async Task<ActionResult<IEnumerable<WineType>>> GetWineType()
26 {
27 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
28 {
29 const string query = $@"
30 select wine_type_id as WineTypeId, wine_type_name as WineTypeName, wine_type_description as WineTypeDescription, wine_type_region as WineTypeRegion
31 from wine_type wt";
32 var wineTypes = await db.QueryAsync<WineType>(query);
33 return Ok(wineTypes);
34 }
35 }
36
37 // GET: api/WineType/5
38 [HttpGet("{id}")]
39 public async Task<ActionResult<WineType>> GetWineType(int id)
40 {
41 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
42 {
43 var parameters = new { id = id };
44
45 string query = $@"
46 select wine_type_id as WineTypeId, wine_type_name as WineTypeName, wine_type_description as WineTypeDescription, wine_type_region as WineTypeRegion
47 from wine_type wt
48 where wine_type_id = @id";
49
50 var wineType = await db.QueryFirstAsync<WineType>(query, parameters);
51 return Ok(wineType);
52 }
53 }
54
55 // PUT: api/WineType/5
56 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
57 [HttpPut]
58 public async Task<IActionResult> PutWineType([FromBody] WineType wineType)
59 {
60 try
61 {
62 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
63 {
64 db.Open();
65 using (var transaction = db.BeginTransaction()) // Start a transaction
66 {
67 var parameters = new {
68 WineTypeName = wineType.WineTypeName.Trim(),
69 WineTypeDescription = wineType.WineTypeDescription.Trim(),
70 WineTypeId = wineType.WineTypeId,
71 WineTypeRegion = wineType.WineTypeRegion.Trim()
72 };
73
74 await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Wine_Type", parameters.WineTypeName, entityId: parameters.WineTypeId);
75
76 string query = $@"
77 update wine_type
78 set
79 wine_type_name = @WineTypeName,
80 wine_type_description = @WineTypeDescription,
81 wine_type_region = @WineTypeRegion
82 where wine_type_id = @WineTypeId";
83
84 await db.ExecuteAsync(query, parameters, transaction: transaction);
85
86 transaction.Commit();
87 return Ok();
88 }
89 }
90 }
91 catch (Exception ex)
92 {
93 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
94 }
95 }
96
97 // POST: api/WineType
98 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
99 [HttpPost]
100 public async Task<IActionResult> PostWineType([FromBody] WineType wineType)
101 {
102 try
103 {
104 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
105 {
106 db.Open();
107 using (var transaction = db.BeginTransaction()) // Start a transaction
108 {
109 var parameters = new {
110 WineTypeName = wineType.WineTypeName.Trim(),
111 WineTypeDescription = wineType.WineTypeDescription.Trim(),
112 WineTypeRegion = wineType.WineTypeRegion.Trim()
113 };
114
115 await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Wine_Type", parameters.WineTypeName);
116
117 string query = "INSERT INTO wine_type (wine_type_name, wine_type_description, wine_type_region) VALUES (@WineTypeName, @WineTypeDescription, @WineTypeRegion);";
118 await db.ExecuteAsync(query, parameters, transaction: transaction);
119
120 transaction.Commit();
121 return Ok();
122 }
123 }
124 }
125 catch (Exception ex)
126 {
127 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
128 }
129 }
130
131 // DELETE: api/WineType/5
132 [HttpDelete("{id}")]
133 public async Task<IActionResult> DeleteWineType(int id)
134 {
135 try
136 {
137 //Check if there is a wine linked to the wine type we are trying to delete, if so return a message and dont let the user delete the wine type.
138 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
139 {
140 db.Open();
141 using (var transaction = db.BeginTransaction()) // Start a transaction
142 {
143 var parameters = new { id = id };
144
145 string wineTypeLinkToWineQuery = $@"
146 select count(*)
147 from wine_type wt
148 join wine w on wt.wine_type_id = w.wine_type_id
149 where w.wine_type_id = @id";
150
151 var isWineTypeToWine = await db.QueryFirstAsync<int>(wineTypeLinkToWineQuery, parameters, transaction: transaction);
152 if (isWineTypeToWine > 0)
153 return BadRequest(new ErrorHandler { Name = "Wine Type Cannot Be Deleted", Description = "Wine Type cannot be deleted because it is linked to a customer" });
154
155 string query = $@"delete from wine_type where wine_type_id = @id";
156 await db.ExecuteAsync(query, parameters, transaction: transaction);
157
158 transaction.Commit();
159 return Ok();
160 }
161 }
162 }
163 catch (Exception ex)
164 {
165 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
166 }
167 }
168 }
169}
Note: See TracBrowser for help on using the repository browser.