source: WineTrackerFinal/WineTrackerWebApi/Controllers/CustomerTypeController.cs

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

initial commit WineTracker Project

  • Property mode set to 100644
File size: 7.1 KB
Line 
1using System.Data;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using Npgsql;
5using Dapper;
6using WineTrackerWebApi.Models.CustomerType;
7using WineTrackerWebApi.Models.ErrorHandle;
8using WineTrackerWebApi.Helpers;
9
10namespace WineTrackerWebApi.Controllers
11{
12 [Route("api/[controller]")]
13 [ApiController]
14 public class CustomerTypeController : ControllerBase
15 {
16 private readonly string _connectionString;
17
18 public CustomerTypeController(IConfiguration configuration)
19 {
20 _connectionString = configuration.GetConnectionString("DefaultConnection");
21 }
22
23 // GET: api/CustomerType
24 [HttpGet]
25 public async Task<ActionResult<IEnumerable<CustomerType>>> GetCustomerType()
26 {
27 using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
28 {
29 const string query = $@"
30 select customer_type_id as CustomerTypeId, customer_type_name as CustomerTypeName, customer_type_description as CustomerTypeDescription
31 from customer_type ct";
32 var customerTypes = await db.QueryAsync<CustomerType>(query);
33 return Ok(customerTypes);
34 }
35 }
36
37 // GET: api/CustomerType/5
38 [HttpGet("{id}")]
39 public async Task<ActionResult<CustomerType>> GetCustomerType(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 customer_type_id as CustomerTypeId, customer_type_name as CustomerTypeName, customer_type_description as CustomerTypeDescription
47 from customer_type ct
48 where customer_type_id = @id";
49
50 var customerType = await db.QueryFirstAsync<CustomerType>(query, parameters);
51 return Ok(customerType);
52 }
53 }
54
55 // PUT: api/CustomerType/5
56 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
57 [HttpPut]
58 public async Task<IActionResult> PutCustomerType([FromBody] CustomerType customerType)
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 { CustomerTypeName = customerType.CustomerTypeName.Trim(), CustomerTypeDescription = customerType.CustomerTypeDescription.Trim(), CustomerTypeId = customerType.CustomerTypeId };
68
69 await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Customer_Type", parameters.CustomerTypeName, entityId: parameters.CustomerTypeId);
70
71 string query = $@"
72 update customer_type
73 set
74 customer_type_name = @CustomerTypeName,
75 customer_type_description = @CustomerTypeDescription
76 where customer_type_id = @CustomerTypeId";
77
78 var updatedColumns = 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/CustomerType
92 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
93 [HttpPost]
94 public async Task<IActionResult> PostCustomerType([FromBody] CustomerType customerType)
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 { CustomerTypeName = customerType.CustomerTypeName.Trim(), CustomerTypeDescription = customerType.CustomerTypeDescription.Trim() };
104
105 await ModelValidation.DoesEntityExistByName((NpgsqlConnection)db, (NpgsqlTransaction)transaction, "Customer_Type", parameters.CustomerTypeName);
106
107 string query = "INSERT INTO customer_type (customer_type_name, customer_type_description) VALUES (@CustomerTypeName, @CustomerTypeDescription);";
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/CustomerType/5
122 [HttpDelete("{id}")]
123 public async Task<IActionResult> DeleteCustomerType(int id)
124 {
125 try
126 {
127 //Check if there is a customer linked to the customer type we are trying to delete, if so return a message and dont let the user delete the customer 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 customerTypeLinkToCustomerQuery = $@"
136 select count(*)
137 from customer_type ct
138 join customer c on ct.customer_type_id = c.customer_type_id
139 where c.customer_type_id = @id";
140
141 var isCustomerTypeLinkedToCustomer = await db.QueryFirstAsync<int>(customerTypeLinkToCustomerQuery, parameters, transaction: transaction);
142
143 if (isCustomerTypeLinkedToCustomer > 0)
144 return BadRequest(new ErrorHandler { Name = "Customer Type Cannot Be Deleted", Description = "Customer Type cannot be deleted because it is linked to a customer" });
145
146 string query = $@"delete from customer_type where customer_type_id = @id";
147 await db.ExecuteAsync(query, parameters, transaction: transaction);
148
149 transaction.Commit();
150 return Ok();
151 }
152 }
153 }
154 catch (Exception ex)
155 {
156 return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
157 }
158 }
159 }
160}
Note: See TracBrowser for help on using the repository browser.