source: WineTrackerFinal/WineTrackerWebApi/Helpers/ModelValidation.cs@ 17d6948

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

initial commit WineTracker Project

  • Property mode set to 100644
File size: 4.6 KB
Line 
1using Dapper;
2using Microsoft.EntityFrameworkCore.Metadata;
3using Microsoft.EntityFrameworkCore.Metadata.Internal;
4using Npgsql;
5using WineTrackerWebApi.Models.Employee;
6using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
7
8namespace WineTrackerWebApi.Helpers
9{
10 public static class ModelValidation
11 {
12 /// <summary>
13 /// Checks if an entity exists in the database with the same name as the one passed in.
14 /// The entity name should be unique.
15 /// </summary>
16 /// <returns></returns>
17 public static async Task DoesEntityExistByName(NpgsqlConnection db, NpgsqlTransaction transaction, string dbTable, string valueToCheck, string columnName = "", int entityId = 0)
18 {
19 var parameters = new { valueToCheck = valueToCheck.ToLower() };
20
21 string EntityExistsinDbQuery = $@"select count(*) from {dbTable} where
22 lower({(string.IsNullOrEmpty(columnName) ? dbTable + "_name" : columnName)}) = @valueToCheck
23 {(entityId != 0 ? $"and {dbTable}_id != {entityId}":"")}";
24
25 var doesEntityExistInDb = await db.QueryFirstAsync<int>(EntityExistsinDbQuery, parameters, transaction: transaction);
26
27 if (doesEntityExistInDb > 0)
28 throw new Exception("The entity with that Name already exists.");
29 }
30
31 /// <summary>
32 /// Checks if an employee exists in the database with the same name and surname as the one passed in.
33 /// The employee name and surname should be unique.
34 /// </summary>
35 /// <returns></returns>
36 public static async Task DoesEmployeeExist(NpgsqlConnection db, NpgsqlTransaction transaction, string name, string surname, int employeeId = 0)
37 {
38 var parameters = new { name = name.ToLower(), surname = surname.ToLower() };
39
40 string EntityExistsinDbQuery = $@"select count(*) from Employee where lower(employee_name) = @name and lower(employee_surname) = @surname
41 {(employeeId != 0 ? $"and employee_id != {employeeId}" : "")}";
42
43 var doesEntityExistInDb = await db.QueryFirstAsync<int>(EntityExistsinDbQuery, parameters, transaction: transaction);
44
45 if (doesEntityExistInDb > 0)
46 throw new Exception("The Employee with the given Name and Surname already exists.");
47 }
48
49 /// <summary>
50 /// Checks if the vehicle details already exist.
51 /// The entity name should be unique.
52 /// </summary>
53 /// <returns></returns>
54 public static async Task DoVehicleDetailsExist(NpgsqlConnection db, NpgsqlTransaction transaction, string make, string model, int capacity, int vehicleDetailsId = 0)
55 {
56 var parameters = new { make = make.ToLower(), model = model.ToLower(), capacity = capacity };
57
58 string EntityExistsinDbQuery = $@"select count(*) from vehicle_details where lower(make) = @make and lower(model) = @model and capacity = @capacity
59 {(vehicleDetailsId != 0 ? $"and vehicle_details_id != {vehicleDetailsId}" : "")}";
60
61 var doesEntityExistInDb = await db.QueryFirstAsync<int>(EntityExistsinDbQuery, parameters, transaction: transaction);
62
63 if (doesEntityExistInDb > 0)
64 throw new Exception("The Vehicle Details already exist.");
65 }
66
67 /// <summary>
68 /// Checks if the vehicle is already booked for the day.
69 /// </summary>
70 /// <param name="db"></param>
71 /// <param name="transaction"></param>
72 /// <param name="vehicleId"></param>
73 /// <param name="shipmentDate"></param>
74 /// <returns></returns>
75 /// <exception cref="Exception"></exception>
76 public static async Task IsVehicleAlreadyBooked(NpgsqlConnection db, NpgsqlTransaction transaction, int vehicleId, DateTime shipmentDate, int entityId = 0)
77 {
78 var parameters = new { vehicleId = vehicleId, shipmentDate = shipmentDate };
79
80 string EntityExistsinDbQuery = $@"select count(*) from shipment where Vehicle_Id = @VehicleId and Shipment_Date = @ShipmentDate
81 {(entityId != 0 ? $"and shipment_id != {entityId}" : "")}";
82
83 var doesEntityExistInDb = await db.QueryFirstAsync<int>(EntityExistsinDbQuery, parameters, transaction: transaction);
84
85 if (doesEntityExistInDb > 0)
86 throw new Exception("The Vehicle is already booked for the choosen Date. Please choose another vehicle or change the Date.");
87 }
88 }
89}
Note: See TracBrowser for help on using the repository browser.