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.ExpenseType;
|
---|
9 |
|
---|
10 | namespace WineTrackerWebApi.Controllers
|
---|
11 | {
|
---|
12 | [Route("api/[controller]")]
|
---|
13 | [ApiController]
|
---|
14 | public class ExpenseTypeController : ControllerBase
|
---|
15 | {
|
---|
16 | private readonly string _connectionString;
|
---|
17 |
|
---|
18 | public ExpenseTypeController(IConfiguration configuration)
|
---|
19 | {
|
---|
20 | _connectionString = configuration.GetConnectionString("DefaultConnection");
|
---|
21 | }
|
---|
22 |
|
---|
23 | // GET: api/ExpenseType
|
---|
24 | [HttpGet]
|
---|
25 | public async Task<ActionResult<IEnumerable<ExpenseType>>> GetExpenseType()
|
---|
26 | {
|
---|
27 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
28 | {
|
---|
29 | const string query = $@"
|
---|
30 | select expense_type_id as ExpenseTypeId, expense_type_name as ExpenseTypeName, expense_type_description as ExpenseTypeDescription
|
---|
31 | from expense_type et";
|
---|
32 | var expenseTypes = await db.QueryAsync<ExpenseType>(query);
|
---|
33 | return Ok(expenseTypes);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | // GET: api/ExpenseType/5
|
---|
38 | [HttpGet("{id}")]
|
---|
39 | public async Task<ActionResult<ExpenseType>> GetExpenseType(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 expense_type_id as ExpenseTypeId, expense_type_name as ExpenseTypeName, expense_type_description as ExpenseTypeDescription
|
---|
47 | from expense_type et
|
---|
48 | where expense_type_id = @id";
|
---|
49 |
|
---|
50 | var expenseType = await db.QueryFirstAsync<ExpenseType>(query, parameters);
|
---|
51 | return Ok(expenseType);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // PUT: api/ExpenseType/5
|
---|
56 | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
---|
57 | [HttpPut]
|
---|
58 | public async Task<IActionResult> PutExpenseType([FromBody] ExpenseType expenseType)
|
---|
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 { ExpenseTypeName = expenseType.ExpenseTypeName.Trim(), ExpenseTypeDescription = expenseType.ExpenseTypeDescription.Trim(), ExpenseTypeId = expenseType.ExpenseTypeId };
|
---|
68 |
|
---|
69 | await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Expense_Type", parameters.ExpenseTypeName, entityId: parameters.ExpenseTypeId);
|
---|
70 |
|
---|
71 | string query = $@"
|
---|
72 | update expense_type
|
---|
73 | set
|
---|
74 | expense_type_name = @ExpenseTypeName,
|
---|
75 | expense_type_description = @ExpenseTypeDescription
|
---|
76 | where expense_type_id = @ExpenseTypeId";
|
---|
77 |
|
---|
78 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
79 |
|
---|
80 | transaction.Commit();
|
---|
81 | return Ok();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | catch (Exception ex)
|
---|
86 | {
|
---|
87 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | // POST: api/ExpenseType
|
---|
92 | // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
---|
93 | [HttpPost]
|
---|
94 | public async Task<IActionResult> PostExpenseType([FromBody] ExpenseType expenseType)
|
---|
95 | {
|
---|
96 | try
|
---|
97 | {
|
---|
98 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
99 | {
|
---|
100 | db.Open();
|
---|
101 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
102 | {
|
---|
103 | var parameters = new { ExpenseTypeName = expenseType.ExpenseTypeName.Trim(), ExpenseTypeDescription = expenseType.ExpenseTypeDescription.Trim() };
|
---|
104 |
|
---|
105 | await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Expense_Type", parameters.ExpenseTypeName);
|
---|
106 |
|
---|
107 | string query = "INSERT INTO expense_type (expense_type_name, expense_type_description) VALUES (@ExpenseTypeName, @ExpenseTypeDescription);";
|
---|
108 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
109 |
|
---|
110 | transaction.Commit();
|
---|
111 | return Ok();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | catch (Exception ex)
|
---|
116 | {
|
---|
117 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | // DELETE: api/ExpenseType/5
|
---|
122 | [HttpDelete("{id}")]
|
---|
123 | public async Task<IActionResult> DeleteExpenseType(int id)
|
---|
124 | {
|
---|
125 | try
|
---|
126 | {
|
---|
127 | //Check if there is a shipment which has an expense of this type, if so return a message and dont let the user delete the expense type.
|
---|
128 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
129 | {
|
---|
130 | db.Open();
|
---|
131 | using (var transaction = db.BeginTransaction()) // Start a transaction
|
---|
132 | {
|
---|
133 | var parameters = new { id = id };
|
---|
134 |
|
---|
135 | string expenseTypeLinkToShipmentQuery = $@"
|
---|
136 | select count(*)
|
---|
137 | from expense_type et
|
---|
138 | join shipment_has_expense_of_expense_type sheoet on et.expense_type_id = sheoet.expense_type_id
|
---|
139 | where et.expense_type_id = @id";
|
---|
140 |
|
---|
141 | var isExpenseTypeLinkedToShipment = await db.QueryFirstAsync<int>(expenseTypeLinkToShipmentQuery, parameters, transaction: transaction);
|
---|
142 | if (isExpenseTypeLinkedToShipment > 0)
|
---|
143 | return BadRequest(new ErrorHandler { Name = "Expense Type Cannot Be Deleted", Description = "Expense Type cannot be deleted because a Shipment has an expense of that type." });
|
---|
144 |
|
---|
145 | string query = $@"delete from expense_type where expense_type_id = @id";
|
---|
146 | await db.ExecuteAsync(query, parameters, transaction: transaction);
|
---|
147 |
|
---|
148 | transaction.Commit();
|
---|
149 | return Ok();
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | catch (Exception ex)
|
---|
154 | {
|
---|
155 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|