Skip to content

Commit

Permalink
Implementation of operations of PlanoConta (#13)
Browse files Browse the repository at this point in the history
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
disouzam authored Dec 19, 2024
2 parents 5e41479 + f811a51 commit 6981611
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Diagnostics;
using System.Linq;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

using myfinance.web.Infrastructure;
Expand All @@ -26,13 +24,6 @@ MyFinanceDbContext myFinanceDbContext

public IActionResult Index()
{
var firstRecordForTesting = myFinanceDbContext.PlanoConta.FirstOrDefault();

if (firstRecordForTesting != null)
{
ViewBag.Teste = firstRecordForTesting.Nome;
}

var resultView = View();
return resultView;
}
Expand Down
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 myfinance-web-wo-top-level-statement/Models/PlanoContaModel.cs
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; }
}
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;
}
}
2 changes: 2 additions & 0 deletions myfinance-web-wo-top-level-statement/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Hosting;

using myfinance.web.Infrastructure;
using myfinance.web.Services;

namespace myfinance.web;

Expand All @@ -15,6 +16,7 @@ public static void Main(string[] args)
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<MyFinanceDbContext>();
builder.Services.AddScoped<IPlanoContaService, PlanoContaService>();

var app = builder.Build();

Expand Down
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 myfinance-web-wo-top-level-statement/Services/PlanoContaService.cs
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();
}
}
7 changes: 3 additions & 4 deletions myfinance-web-wo-top-level-statement/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
<div class="text-center">
<h1 class="display-4">My Finance Web App</h1>
<p>
Uma aplicação construída por
<a href="https://www.linkedin.com/in/disouzam" target="_blank" rel="noopener">
Uma aplicação construída por
<a href="https://www.linkedin.com/in/disouzam" target="_blank" rel="noopener">
Dickson Souza
</a>
</a>
como requisito parcial para aprovação <br />na disciplina Práticas de Implementação de Software <br />do curso de Pós-Graduação em Engenharia de Software <br />da PUC Minas, Oferta 7/2024, <br /> professor
<a href="https://www.linkedin.com/in/filipe-nhimi-a16a0836/" target="_blank" rel="noopener">
Filipe Torio
</a>.
</p>
<h2>@ViewBag.Teste</h2>
</div>
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 myfinance-web-wo-top-level-statement/Views/PlanoConta/Index.cshtml
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>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
Home
</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark"
style="color: white !important"
asp-area=""
asp-controller="PlanoConta"
asp-action="Index">
Plano de Contas
</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark"
style="color: white !important"
Expand Down

0 comments on commit 6981611

Please sign in to comment.