Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teste/Caio-Cesar-Moraes-Costa #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
31 changes: 31 additions & 0 deletions PostgreAPI/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net7.0/PostgreAPI.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"externalConsole": true,
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
46 changes: 46 additions & 0 deletions PostgreAPI/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/PostgreAPI.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}

},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/PostgreAPI.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/PostgreAPI.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
42 changes: 42 additions & 0 deletions PostgreAPI/ConnectionString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace PostgreAPI
{
public class ConnectionString
{
private readonly string? host;
private readonly string? database;
private readonly string? user;
private readonly string? password;

public ConnectionString()
{
Console.Write("Host: ");
this.host = Console.ReadLine();
if(host == null)
host = "DEFAULT";

Console.Write("Database: ");
this.database = Console.ReadLine();
if(database == null)
database = "DEFAULT";

Console.Write("User: ");
this.user = Console.ReadLine();
if(user == null)
user = "DEFAULT";

Console.Write("Password: ");
this.password = Console.ReadLine();
if(password == null)
password = "DEFAULT";

Console.Clear();
}

public override string ToString()
{
return $"Host={host};Database={database};Username={user};Password={password}";
}
}
}
116 changes: 116 additions & 0 deletions PostgreAPI/Controllers/EquipmentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PostgreAPI.Models;

namespace PostgreAPI.Controllers
{
[ApiController]
public class EquipmentController : ControllerBase
{
[HttpPost]
[Route("/Equipment/Insert")]
public Guid InsertEquipment(string Name, Guid Model_Id)
{
try
{
AikoAPIContext context = new AikoAPIContext();

if (context.EquipmentModels.Where(x => x.Id == Model_Id).FirstOrDefault() == null)
throw new Exception("Model_Id é inválido!");

if (Name.Trim().Count() <= 0)
throw new Exception("Name é inválido!");


Guid id = Guid.NewGuid();

Equipment equipment = new Equipment();
equipment.Name = Name;
equipment.Id = id;
equipment.Model_Id = Model_Id;

context.Equipment.Add(equipment);
context.SaveChanges();

return id;
}
catch (Exception ex)
{
throw ex;
}
}

[HttpGet]
[Route("/Equipment/Get")]
public List<Equipment> GetEquipment()
{
try
{
AikoAPIContext context = new AikoAPIContext();

List<Equipment> equipmentList = context.Equipment.ToList();
return equipmentList;
}
catch (Exception ex)
{
throw ex;
}
}

[HttpPut]
[Route("/Equipment/Update")]
public bool UpdateEquipment(Guid id, String name, Guid ModelId)
{
try
{
AikoAPIContext context = new AikoAPIContext();

if (context.EquipmentModels.Where(x => x.Id == ModelId).FirstOrDefault() == null)
throw new Exception("Model_Id é inválido!");

if (name.Trim().Count() <= 0)
throw new Exception("Name é inválido!");

Equipment? equipment = context.Equipment.Where(x => x.Id == id).FirstOrDefault();

if (equipment == null)
throw new Exception("EquipmentId é inválido!");

equipment.Name = name;
equipment.Model_Id = ModelId;

context.Equipment.Attach(equipment);
context.Entry(equipment).State = EntityState.Modified;
context.SaveChanges();
return true;
}
catch (Exception ex)
{
throw ex;
}
}

[HttpDelete]
[Route("/Equipment/Delete")]
public bool DeleteEquipment(Guid id)
{
try
{
AikoAPIContext context = new AikoAPIContext();

Equipment? equipment = context.Equipment.Where(x => x.Id == id).FirstOrDefault();

if (equipment == null)
throw new Exception("EquipmentId é inválido!");

context.Equipment.Remove(equipment);
context.SaveChanges();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
110 changes: 110 additions & 0 deletions PostgreAPI/Controllers/EquipmentModelController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PostgreAPI.Models;

namespace PostgreAPI.Controllers
{
[ApiController]
public class EquipmentModelController : ControllerBase
{
[HttpPost]
[Route("/EquipmentModel/Insert")]
public Guid InsertEquipmentModel(string name)
{
try
{
AikoAPIContext context = new AikoAPIContext();


if (name.Trim().Count() <= 0)
throw new Exception("Name é inválido!");

Guid id = Guid.NewGuid();

EquipmentModel equipmentModel = new EquipmentModel();
equipmentModel.Name = name;
equipmentModel.Id = id;


context.EquipmentModels.Add(equipmentModel);
context.SaveChanges();

return id;
}
catch (Exception ex)
{
throw ex;
}
}

[HttpGet]
[Route("/EquipmentModel/Get")]
public List<EquipmentModel> GetEquipmentModel()
{
try
{
AikoAPIContext contextModel = new AikoAPIContext();
List<EquipmentModel> equipmentModelList = contextModel.EquipmentModels.ToList();
return equipmentModelList;
}
catch (Exception ex)
{
throw ex;
}
}


[HttpPut]
[Route("/EquipmentModel/Update")]
public bool UpdateEquipmentModel(Guid id, string name)
{
try
{
AikoAPIContext context = new AikoAPIContext();

if (name.Trim().Count() <= 0)
throw new Exception("Name é inválido!");

EquipmentModel? equipmentModel = context.EquipmentModels.Where(x => x.Id == id).FirstOrDefault();

if (equipmentModel == null)
throw new Exception("EquipmentId é inválido!");

equipmentModel.Name = name;


context.EquipmentModels.Attach(equipmentModel);
context.Entry(equipmentModel).State = EntityState.Modified;
context.SaveChanges();
return true;
}
catch (Exception ex)
{
throw ex;
}
}

[HttpDelete]
[Route("/EquipmentModel/Delete")]
public bool DeleteEquipmentModel(Guid id)
{
try
{
AikoAPIContext context = new AikoAPIContext();

EquipmentModel? equipmentModel = context.EquipmentModels.Where(x => x.Id == id).FirstOrDefault();

if (equipmentModel == null)
throw new Exception("EquipmentId é inválido!");

context.EquipmentModels.Remove(equipmentModel);
context.SaveChanges();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Loading