Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions WebApplication4.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32421.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication4", "WebApplication4\WebApplication4.csproj", "{E95014EE-311D-4740-B3A1-9DA25C9A67B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E95014EE-311D-4740-B3A1-9DA25C9A67B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E95014EE-311D-4740-B3A1-9DA25C9A67B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E95014EE-311D-4740-B3A1-9DA25C9A67B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E95014EE-311D-4740-B3A1-9DA25C9A67B4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CCFE820B-0A9D-4F01-BF0F-B6FD2D2F61A3}
EndGlobalSection
EndGlobal
246 changes: 246 additions & 0 deletions WebApplication4/Controllers/RegistrationsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Npgsql;
using System.Data;
using System.Net;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication4.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegistrationsController : ControllerBase
{
private IConfiguration _configuration;

public RegistrationsController(IConfiguration configuration)
{
_configuration = configuration;
}


// GET: api/<RegistrationsController>
[HttpGet]
public string Get()
{
DataTable table = new DataTable();
NpgsqlDataReader myReader;

using (NpgsqlConnection myConn = new NpgsqlConnection(_configuration.GetConnectionString("DataBaseConfig")))
{
myConn.Open();
using (NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM registrations;", myConn))
{
myReader = command.ExecuteReader();
table.Load(myReader);

myReader.Close();
myConn.Close();
}
}

return JsonConvert.SerializeObject(table);
}

// GET api/<RegistrationsController>/5
[HttpGet("{id}")]
public string Get(int id)
{
DataTable table = new DataTable();
NpgsqlDataReader myReader;

using(NpgsqlConnection myConn = new NpgsqlConnection(_configuration.GetConnectionString("DataBaseConfig")))
{
myConn.Open();
using(NpgsqlCommand command = new NpgsqlCommand(String.Format("SELECT * FROM registrations WHERE id = {0};",id),myConn))
{
myReader = command.ExecuteReader();
table.Load(myReader);

myReader.Close();
myConn.Close();

}
}

if (table.Rows.Count == 0)
return JsonConvert.SerializeObject(new { status = "400", message = "invalid id" });


return JsonConvert.SerializeObject(table);
}

[HttpGet("search")]
public string Get([FromQuery] string? sort_by = "", [FromQuery] string? sort_type = "ASC", [FromQuery] string? search_string = "")
{
DataTable table = new DataTable();
HttpResponseMessage response = new HttpResponseMessage();
NpgsqlDataReader myReader;
string query = "SELECT * FROM registrations ";

if (sort_by.Length > 0)
query+=String.Format("ORDER BY {0} {1};", sort_by, sort_type);

using (NpgsqlConnection myConn = new NpgsqlConnection(_configuration.GetConnectionString("DataBaseConfig")))
{
myConn.Open();
using (NpgsqlCommand command = new NpgsqlCommand(query, myConn))
{
try
{
myReader = command.ExecuteReader();
}catch (Exception ex)
{
return JsonConvert.SerializeObject(new {status = "200", message = "invalid params"});
}

table.Load(myReader);

myReader.Close();
myConn.Close();

}
}

if(search_string.Length < 1)
{
return JsonConvert.SerializeObject(table);
}


for (int i = 0; i < table.Rows.Count; i++)
{
bool deleteRow = true;

for(int j = 0; j < table.Columns.Count; ++j)
{
if(table.Rows[i][j].ToString().Contains(search_string))
{
deleteRow = false;
}
}


if (deleteRow)
{
table.Rows.Remove(table.Rows[i]);
i--;
}
}
return JsonConvert.SerializeObject(table);

}

// POST api/<RegistrationsController>
[HttpPost]
public string Post([FromBody] REGISTRATION_CERTIFICATE value)
{
try
{
value.validateWholeClass();
}catch (Exception ex)
{
return JsonConvert.SerializeObject(new { status = "400", message = ex.Message });
}

int count = 0;
using (NpgsqlConnection myConn = new NpgsqlConnection(_configuration.GetConnectionString("DataBaseConfig")))
{
string guery = String.Format("INSERT INTO registrations (registration_number,vin_code,car,date_of_registration,year_of_manufacture)" +
"VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')", value.registration_number, value.vin_code, value.car, value.date_of_registration, value.year_of_manufacture);

myConn.Open();
using (NpgsqlCommand command = new NpgsqlCommand(guery, myConn))
{
try
{
count = command.ExecuteNonQuery();
}catch(Exception ex)
{
return JsonConvert.SerializeObject(new { status = "400", message = "Invalid values." });
}

myConn.Close();

}

}

if (count < 1)
{
return JsonConvert.SerializeObject(new { status = "200", message = "invalid id" });
}

return JsonConvert.SerializeObject(new { status = "200", message = "Customer has been successfully added" });
}

// PUT api/<RegistrationsController>/5
[HttpPut("{id}")]
public string Put(int id, [FromBody] REGISTRATION_CERTIFICATE value)
{
try
{
value.validateWholeClass();
}
catch (Exception ex)
{
return JsonConvert.SerializeObject(new { status = "400", message = ex.Message });
}

int count = 0;
using (NpgsqlConnection myConn = new NpgsqlConnection(_configuration.GetConnectionString("DataBaseConfig")))
{
string guery = String.Format("update registrations set registration_number = '{0}' , vin_code = '{1}' ," +
" car = '{2}' , date_of_registration = '{3}' , year_of_manufacture = '{4}' " +
"WHERE id = {5};", value.registration_number, value.vin_code, value.car, value.date_of_registration, value.year_of_manufacture, id);

myConn.Open();
using (NpgsqlCommand command = new NpgsqlCommand(guery, myConn))
{
count = command.ExecuteNonQuery();


myConn.Close();

}

}

if (count < 1)
{
return JsonConvert.SerializeObject(new { status = "400", message = "invalid id" });
}

return JsonConvert.SerializeObject(new { status = "200", message = "Customer has been successfully updated" });
}

// DELETE api/<RegistrationsController>/5
[HttpDelete("{id}")]
public Task<HttpResponseMessage> Delete(int id)
{
int count = 0;
using (NpgsqlConnection myConn = new NpgsqlConnection(_configuration.GetConnectionString("DataBaseConfig")))
{
myConn.Open();
using (NpgsqlCommand command = new NpgsqlCommand(String.Format("DELETE FROM registrations WHERE id = {0};", id), myConn))
{
count = command.ExecuteNonQuery();
myConn.Close();

}
}
HttpResponseMessage response = new HttpResponseMessage();
if (count < 1)
{
response.StatusCode = HttpStatusCode.BadRequest;
response.Content = new StringContent("Customer is not found");
}else
{
response.StatusCode = HttpStatusCode.OK;
response.Content = new StringContent("Customer has been successfully deleted.");
}
return Task.FromResult(response);
}
}
}
25 changes: 25 additions & 0 deletions WebApplication4/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
31 changes: 31 additions & 0 deletions WebApplication4/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:29940",
"sslPort": 44344
}
},
"profiles": {
"WebApplication4": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7134;http://localhost:5134",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading