-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of operations of PlanoConta (#13)
1. **fa091f1** Reverted change in commit 44d64c8 - Basic configuration to display one entry of database into home page to verify that Entity Framework is properly configured 1. **3dd0c1c** Basic scaffolding of PlanoConta controller and view New files created: - myfinance-web-wo-top-level-statement/Controllers/PlanoContaController.cs - myfinance-web-wo-top-level-statement/Views/PlanoConta/Index.cshtml 1. **f977280** Scaffolding PlanoConta view page 1. **e33515f** Scaffolding service class and interface New files created: - myfinance-web-wo-top-level-statement/Services/IPlanoContaService.cs - myfinance-web-wo-top-level-statement/Services/PlanoContaService.cs 1. **ff19ec2** Configured PlanoConta controller, service and view 1. **de96bbe** Configured route to Cadastro in PlanoConta view New file created: - myfinance-web-wo-top-level-statement/Views/PlanoConta/Cadastro.cshtml 1. **533c237** Completed configuration of Cadastro form 1. **a104a5e** Fixed Cadastro form 1. **87f0952** Implementation of insertion of a new PlanoConta into database records New file created: - myfinance-web-wo-top-level-statement/Models/PlanoContaModel.cs 1. **377eb2d** Fixed issue raised on SonarQube analysis about non-nullable property in `PlanoContaModel` being null after exiting implicit constructor - Also added another filter to call `planoContaService` on PlanoContaController and save an item 1. **0c368dd** Fixed issues found by SonarQube 1. **d5cb581** Extension methods to check validity of PlanoConta instance and to convert between domain model and view model New file created: - myfinance-web-wo-top-level-statement/Models/PlanoContaModelExtensions.cs 1. **f811a51** Refactoring PlanoContaService to check validity of PlanoContaModel using extension methods
- Loading branch information
Showing
11 changed files
with
277 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
myfinance-web-wo-top-level-statement/Controllers/PlanoContaController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using myfinance.web.Domain; | ||
using myfinance.web.Models; | ||
using myfinance.web.Services; | ||
|
||
namespace myfinance.web.Controllers; | ||
|
||
public class PlanoContaController : Controller | ||
{ | ||
private readonly IPlanoContaService planoContaService; | ||
|
||
public PlanoContaController(IPlanoContaService planoContaService) | ||
{ | ||
this.planoContaService = planoContaService; | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
var resultView = View(); | ||
ViewBag.List = planoContaService.ListarRegistros(); | ||
return resultView; | ||
} | ||
|
||
[HttpGet] | ||
[HttpPost] | ||
public IActionResult Cadastro(PlanoContaModel? model) | ||
{ | ||
if (model != null && ModelState.IsValid && this.Request.Method == "POST") | ||
{ | ||
planoContaService.Salvar(model); | ||
} | ||
|
||
var resultView = View(); | ||
return resultView; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
myfinance-web-wo-top-level-statement/Models/PlanoContaModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace myfinance.web.Models; | ||
|
||
public class PlanoContaModel | ||
{ | ||
public int? Id { get; set; } | ||
|
||
public string? Nome { get; set; } | ||
|
||
public string? Tipo { get; set; } | ||
} |
42 changes: 42 additions & 0 deletions
42
myfinance-web-wo-top-level-statement/Models/PlanoContaModelExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using myfinance.web.Domain; | ||
|
||
namespace myfinance.web.Models; | ||
|
||
public static class PlanoContaModelExtensions | ||
{ | ||
public static bool IsValid(this PlanoContaModel planoContaModel) | ||
{ | ||
if (planoContaModel == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (planoContaModel.Nome == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (planoContaModel.Tipo == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static PlanoConta? ConvertToPlanoConta(this PlanoContaModel model) | ||
{ | ||
if (model.Nome == null || model.Tipo == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var item = new PlanoConta() | ||
{ | ||
Nome = model.Nome, | ||
Tipo = model.Tipo | ||
}; | ||
|
||
return item; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
myfinance-web-wo-top-level-statement/Services/IPlanoContaService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Collections.Generic; | ||
|
||
using myfinance.web.Domain; | ||
using myfinance.web.Models; | ||
|
||
namespace myfinance.web.Services; | ||
|
||
public interface IPlanoContaService | ||
{ | ||
List<PlanoConta> ListarRegistros(); | ||
|
||
void Salvar(PlanoContaModel requestItem); | ||
|
||
void Excluir(int id); | ||
|
||
PlanoConta RetornarRegistro(int id); | ||
} |
75 changes: 75 additions & 0 deletions
75
myfinance-web-wo-top-level-statement/Services/PlanoContaService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
using myfinance.web.Controllers; | ||
using myfinance.web.Domain; | ||
using myfinance.web.Infrastructure; | ||
using myfinance.web.Models; | ||
|
||
namespace myfinance.web.Services; | ||
|
||
public class PlanoContaService : IPlanoContaService | ||
{ | ||
private readonly MyFinanceDbContext myFinanceDbContext; | ||
|
||
public PlanoContaService( | ||
ILogger<HomeController> logger, | ||
MyFinanceDbContext myFinanceDbContext | ||
) | ||
{ | ||
this.myFinanceDbContext = myFinanceDbContext; | ||
} | ||
|
||
public void Excluir(int id) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public List<PlanoConta> ListarRegistros() | ||
{ | ||
var list = myFinanceDbContext.PlanoConta.ToList(); | ||
return list; | ||
} | ||
|
||
public PlanoConta RetornarRegistro(int id) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void Salvar(PlanoContaModel requestItem) | ||
{ | ||
PlanoConta? item; | ||
if (requestItem.IsValid()) | ||
{ | ||
item = requestItem.ConvertToPlanoConta(); | ||
} | ||
else | ||
{ | ||
return; | ||
} | ||
|
||
if (item == null) | ||
{ | ||
return; | ||
} | ||
|
||
var dbSet = myFinanceDbContext.PlanoConta; | ||
|
||
if (requestItem.Id == null) | ||
{ | ||
dbSet.Add(item); | ||
} | ||
else | ||
{ | ||
item.Id = (int)requestItem.Id; | ||
|
||
dbSet.Attach(item); | ||
myFinanceDbContext.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Modified; | ||
} | ||
|
||
myFinanceDbContext.SaveChanges(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
myfinance-web-wo-top-level-statement/Views/PlanoConta/Cadastro.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@model PlanoContaModel | ||
|
||
@{ | ||
ViewData["Title"] = "Cadastro de Plano de Contas"; | ||
} | ||
|
||
<h1>@ViewData["Title"]</h1> | ||
|
||
<form asp-controller="PlanoConta" asp-action="Cadastro" method="post"> | ||
<div class="container"> | ||
<div class="mb-3"> | ||
<input type="hidden" id="txtId" class="form-control" asp-for="Id" /> | ||
<label class="form-label" for="txtId"> | ||
Nome | ||
</label> | ||
<input type="text" id="txtNome" class="form-control" asp-for="Nome" /> | ||
<span asp-validation-for="Nome" class="text-danger"></span> | ||
</div> | ||
<div class="mb-3"> | ||
<label asp-for="Tipo">@Html.RadioButtonFor(m => m.Tipo, "D")Despesa</label> | ||
<label asp-for="Tipo">@Html.RadioButtonFor(m => m.Tipo, "R")Receita</label> | ||
<span asp-validation-for="Tipo" class="text-danger"></span> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<button type="submit" class="btn btn-primary btn-lg">Salvar</button> | ||
<button type="button" class="btn btn-secondary btn-lg" onclick="Voltar()">Cancelar</button> | ||
</div> | ||
</form> | ||
|
||
<script type="text/javascript"> | ||
function Voltar() { | ||
window.location.href = "../PlanoConta/Index"; | ||
} | ||
</script> |
47 changes: 47 additions & 0 deletions
47
myfinance-web-wo-top-level-statement/Views/PlanoConta/Index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@using myfinance.web.Domain | ||
|
||
@{ | ||
ViewData["Title"] = "Plano de Contas"; | ||
} | ||
|
||
<h1>Plano de contas</h1> | ||
|
||
<a | ||
type="button" | ||
class="btn btn-primary" | ||
asp-area="" | ||
asp-controller="PlanoConta" | ||
asp-action="Cadastro" | ||
> | ||
Criar item de Plano de Conta | ||
</a> | ||
<br /> | ||
<br /> | ||
<table class="table table-bordered"> | ||
<tr> | ||
<th>Id</th> | ||
<th>Descrição</th> | ||
<th>Tipo</th> | ||
<th>#</th> | ||
<th>#</th> | ||
</tr> | ||
|
||
@{ | ||
foreach (var item in (List<PlanoConta>)ViewBag.List) | ||
{ | ||
<tr> | ||
<td>@item.Id</td> | ||
<td>@item.Nome</td> | ||
<td>@item.Tipo</td> | ||
<td> | ||
<button type="button" class="btn btn-warning">Editar</button> | ||
</td> | ||
<td> | ||
<button type="button" class="btn btn-danger">Excluir</button> | ||
</td> | ||
</tr> | ||
} | ||
} | ||
|
||
|
||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters