[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.Wine;
|
---|
| 9 |
|
---|
| 10 | namespace WineTrackerWebApi.Controllers
|
---|
| 11 | {
|
---|
| 12 | [Route("api/[controller]")]
|
---|
| 13 | [ApiController]
|
---|
| 14 | public class WineController : ControllerBase
|
---|
| 15 | {
|
---|
| 16 | private readonly string _connectionString;
|
---|
| 17 |
|
---|
| 18 | public WineController(IConfiguration configuration)
|
---|
| 19 | {
|
---|
| 20 | _connectionString = configuration.GetConnectionString("DefaultConnection");
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | // GET: api/Wine
|
---|
| 24 | [HttpGet]
|
---|
| 25 | public async Task<ActionResult<IEnumerable<Wine>>> GetWine()
|
---|
| 26 | {
|
---|
| 27 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 28 | {
|
---|
| 29 | const string query = $@"
|
---|
| 30 | select
|
---|
| 31 | w.wine_id as WineId,
|
---|
| 32 | wt.wine_type_id as WineTypeId,
|
---|
| 33 | w.wine_name as WineName,
|
---|
| 34 | wt.wine_type_name as WineTypeName,
|
---|
| 35 | w.base_price as WineBasePrice,
|
---|
| 36 | TO_CHAR(w.year_produced, 'DD-MM-YYYY') as WineYearProduced
|
---|
| 37 | from wine w
|
---|
| 38 | join wine_type wt on w.wine_type_id = wt.wine_type_id
|
---|
| 39 | ";
|
---|
| 40 | var wines = await db.QueryAsync<Wine>(query);
|
---|
| 41 | return Ok(wines);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | // GET: api/Wine/5
|
---|
| 46 | [HttpGet("{id}")]
|
---|
| 47 | public async Task<ActionResult<Wine>> GetWine(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 | w.wine_id as WineId,
|
---|
| 56 | wt.wine_type_id as WineTypeId,
|
---|
| 57 | w.wine_name as WineName,
|
---|
| 58 | wt.wine_type_name as WineTypeName,
|
---|
| 59 | w.base_price as WineBasePrice,
|
---|
| 60 | TO_CHAR(w.year_produced, 'YYYY-MM-DD') as WineYearProduced
|
---|
| 61 | from wine w
|
---|
| 62 | join wine_type wt on w.wine_type_id = wt.wine_type_id
|
---|
| 63 | where w.wine_id = @id
|
---|
| 64 | ";
|
---|
| 65 | var wine = await db.QueryFirstAsync<Wine>(query, parameters);
|
---|
| 66 | return Ok(wine);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | // PUT: api/Wine/5
|
---|
| 71 | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
---|
| 72 | [HttpPut]
|
---|
| 73 | public async Task<IActionResult> PutWine([FromBody] Wine wine)
|
---|
| 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 | WineName = wine.WineName.Trim(),
|
---|
| 84 | WineTypeId = wine.WineTypeId,
|
---|
| 85 | WineBasePrice = wine.WineBasePrice,
|
---|
| 86 | WineYearProduced = DateTime.Parse(wine.WineYearProduced),
|
---|
| 87 | WineId = wine.WineId
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Wine", parameters.WineName, entityId: parameters.WineId);
|
---|
| 91 |
|
---|
| 92 | string query = $@"
|
---|
| 93 | update wine
|
---|
| 94 | set
|
---|
| 95 | wine_name = @WineName,
|
---|
| 96 | wine_type_id = @WineTypeId,
|
---|
| 97 | base_price = @WineBasePrice,
|
---|
| 98 | year_produced = @WineYearProduced
|
---|
| 99 | where wine_id = @WineId
|
---|
| 100 | ";
|
---|
| 101 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
| 102 |
|
---|
| 103 | transaction.Commit();
|
---|
| 104 | return Ok();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | catch (Exception ex)
|
---|
| 109 | {
|
---|
| 110 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // POST: api/Wine
|
---|
| 115 | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
---|
| 116 | [HttpPost]
|
---|
| 117 | public async Task<IActionResult> PostWine([FromBody] Wine wine)
|
---|
| 118 | {
|
---|
| 119 | try
|
---|
| 120 | {
|
---|
| 121 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 122 | {
|
---|
| 123 | db.Open();
|
---|
| 124 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
| 125 | {
|
---|
| 126 | var parameters = new {
|
---|
| 127 | WineName = wine.WineName.Trim(),
|
---|
| 128 | WineTypeId = wine.WineTypeId,
|
---|
| 129 | WineBasePrice = wine.WineBasePrice,
|
---|
| 130 | WineYearProduced = DateTime.Parse(wine.WineYearProduced)
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Wine", parameters.WineName);
|
---|
| 134 |
|
---|
| 135 | string query = $@"insert into wine (wine_name, wine_type_id, base_price, year_produced) values (@WineName, @WineTypeId, @WineBasePrice, @WineYearProduced)";
|
---|
| 136 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
| 137 |
|
---|
| 138 | transaction.Commit();
|
---|
| 139 | return Ok();
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | catch (Exception ex)
|
---|
| 144 | {
|
---|
| 145 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | // DELETE: api/Wine/5
|
---|
| 150 | [HttpDelete("{id}")]
|
---|
| 151 | public async Task<IActionResult> DeleteWine(int id)
|
---|
| 152 | {
|
---|
| 153 | try
|
---|
| 154 | {
|
---|
| 155 | //Check if there is a shipment load linked to the wine we are trying to delete, if so return a message and dont let the user delete the wine.
|
---|
| 156 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
| 157 | {
|
---|
| 158 | db.Open();
|
---|
| 159 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
| 160 | {
|
---|
| 161 | var parameters = new { id = id };
|
---|
| 162 |
|
---|
| 163 | string wineLinkToShipmentLoadQuery = $@"
|
---|
| 164 | select count(*)
|
---|
| 165 | from shipment_load sl
|
---|
| 166 | where wine_id = @id";
|
---|
| 167 |
|
---|
| 168 | var isWineLinkedToShipmentLoad = await db.QueryFirstAsync<int>(wineLinkToShipmentLoadQuery, parameters, transaction: transaction);
|
---|
| 169 | if (isWineLinkedToShipmentLoad > 0)
|
---|
| 170 | return BadRequest(new ErrorHandler { Name = "Wine Cannot Be Deleted", Description = "Wine cannot be deleted because it is linked to a shipment load" });
|
---|
| 171 |
|
---|
| 172 | string query = $@"delete from wine where wine_Id = @id";
|
---|
| 173 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
| 174 |
|
---|
| 175 | transaction.Commit();
|
---|
| 176 | return Ok();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | catch (Exception ex)
|
---|
| 181 | {
|
---|
| 182 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|