From cf2e20ed8208e5b52fdda6c211652747c8d01ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oziel=20Guimar=C3=A3es=20de=20Paula=20Silva?= Date: Mon, 27 Nov 2023 08:48:51 -0300 Subject: [PATCH] Adicionando migration --- .../Converters/DateTimeToDateOnlyConverter.cs | 7 +- .../20231120192217_DataNascimento.cs | 35 ++++++++++ .../Migrations/MainContextModelSnapshot.cs | 4 +- .../Services/Requests/Aluno/PessoaRequest.cs | 7 +- .../ViewModels/Aluno/NovoAlunoViewModel.cs | 70 +++++++++++++------ 5 files changed, 95 insertions(+), 28 deletions(-) create mode 100644 EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/20231120192217_DataNascimento.cs diff --git a/EscolaBiblicaDominical/Ebd.Application/Converters/DateTimeToDateOnlyConverter.cs b/EscolaBiblicaDominical/Ebd.Application/Converters/DateTimeToDateOnlyConverter.cs index 8597661..fa81b15 100644 --- a/EscolaBiblicaDominical/Ebd.Application/Converters/DateTimeToDateOnlyConverter.cs +++ b/EscolaBiblicaDominical/Ebd.Application/Converters/DateTimeToDateOnlyConverter.cs @@ -9,10 +9,12 @@ public class DateTimeToDateOnlyConverter : JsonConverter public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { string dateString = reader.GetString(); + if (DateTimeOffset.TryParse(dateString, out DateTimeOffset dateTimeOffset)) - { return DateOnly.FromDateTime(dateTimeOffset.DateTime); - } + + if (DateOnly.TryParse(dateString, out DateOnly dateOnly)) + return dateOnly; throw new JsonException("Invalid date format"); } @@ -22,4 +24,5 @@ public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializer writer.WriteStringValue(value.ToString()); } } + } \ No newline at end of file diff --git a/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/20231120192217_DataNascimento.cs b/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/20231120192217_DataNascimento.cs new file mode 100644 index 0000000..68a96c3 --- /dev/null +++ b/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/20231120192217_DataNascimento.cs @@ -0,0 +1,35 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Ebd.Infra.Data.Migrations +{ + /// + public partial class DataNascimento : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "NascidoEm", + table: "Pessoa", + type: "date", + nullable: false, + oldClrType: typeof(DateTime), + oldType: "timestamp with time zone"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "NascidoEm", + table: "Pessoa", + type: "timestamp with time zone", + nullable: false, + oldClrType: typeof(DateOnly), + oldType: "date"); + } + } +} diff --git a/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/MainContextModelSnapshot.cs b/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/MainContextModelSnapshot.cs index 4e0bf4a..9d9906e 100644 --- a/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/MainContextModelSnapshot.cs +++ b/EscolaBiblicaDominical/Ebd.Infra.Data/Migrations/MainContextModelSnapshot.cs @@ -244,8 +244,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("PessoaId")); - b.Property("NascidoEm") - .HasColumnType("timestamp with time zone"); + b.Property("NascidoEm") + .HasColumnType("date"); b.Property("Nome") .IsRequired() diff --git a/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/Services/Requests/Aluno/PessoaRequest.cs b/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/Services/Requests/Aluno/PessoaRequest.cs index 225ae6a..6a0ba9a 100644 --- a/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/Services/Requests/Aluno/PessoaRequest.cs +++ b/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/Services/Requests/Aluno/PessoaRequest.cs @@ -1,5 +1,4 @@ -using Ebd.Mobile.Extensions; -using Ebd.Mobile.Services.Requests.Contato; +using Ebd.Mobile.Services.Requests.Contato; using Ebd.Mobile.Services.Requests.Endereco; using System; using System.Collections.Generic; @@ -12,13 +11,13 @@ public PessoaRequest(string nome, ContatoRequest contato) { Nome = nome; Contatos = new List { contato }; - NascidoEm = DateTime.Now.AddYears(-30).ToDateOnly(); + NascidoEm = DateTime.Now.AddYears(-30); WhatsappIgualCelular = true; } public string Nome { get; set; } public bool WhatsappIgualCelular { get; set; } - public string NascidoEm { get; set; } + public DateTime NascidoEm { get; set; } public ICollection Enderecos { get; set; } public ICollection Contatos { get; set; } diff --git a/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/ViewModels/Aluno/NovoAlunoViewModel.cs b/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/ViewModels/Aluno/NovoAlunoViewModel.cs index aa13246..0df9f73 100644 --- a/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/ViewModels/Aluno/NovoAlunoViewModel.cs +++ b/EscolaBiblicaDominical/Ebd.Mobile/Ebd.Mobile/ViewModels/Aluno/NovoAlunoViewModel.cs @@ -58,12 +58,7 @@ public TurmaResponse TurmaSelecionada { var turmaSelecionadaAnteriormente = turmaSelecionada; SetProperty(ref turmaSelecionada, value); - - if (turmaSelecionada is not null && turmaSelecionada.Equals(turmaSelecionadaAnteriormente).Not()) - { - Debug.WriteLine("test"); - //MainThread.BeginInvokeOnMainThread(() => CarregarListaAlunosCommand.ExecuteAsync(true).ConfigureAwait(true)); ; - } + TurmaAlterada(turmaSelecionadaAnteriormente); } } @@ -148,7 +143,7 @@ public string Email get => email; set { - SetProperty(ref email, value); + SetProperty(ref email, value?.ToLower()); CheckFormIsValid(); } } @@ -159,7 +154,7 @@ public string NomeMae get => nomeMae; set { - SetProperty(ref nomeMae, value); + SetProperty(ref nomeMae, value?.ToTitleCase()); CheckFormIsValid(); } } @@ -181,7 +176,7 @@ public string NomePai get => nomePai; set { - SetProperty(ref nomePai, value); + SetProperty(ref nomePai, value?.ToTitleCase()); CheckFormIsValid(); } } @@ -277,10 +272,6 @@ private async Task SalvarCommandExecute() if (IsValid) { var enderecoAluno = new EnderecoRequest(Logradouro, Numero, cep: Cep, BairroId); - var responsavelMae = new PessoaRequest(NomeMae, new ContatoRequest(CelularMae, TipoContato.Celular)) - { - Enderecos = new List { enderecoAluno } - }; var responsaveis = new List { @@ -308,7 +299,7 @@ private async Task SalvarCommandExecute() var alunoRequest = new AlterarAlunoRequest { NascidoEm = DataNascimento.Value, - Nome = Nome, + Nome = Nome.ToTitleCase(), WhatsappIgualCelular = true, AlunoId = AlunoId, TurmaId = TurmaSelecionada.TurmaId, @@ -319,6 +310,7 @@ private async Task SalvarCommandExecute() }, Enderecos = new List { enderecoAluno }, Responsaveis = responsaveis + Responsaveis = ObterResponsaveis(enderecoAluno) }; var response = await _alunoService.SalvarAsync(alunoRequest); if (response.IsSuccess) @@ -341,6 +333,39 @@ private async Task SalvarCommandExecute() throw; } } + + private List ObterResponsaveis(EnderecoRequest enderecoAluno) + { + var responsaveis = new List(); + + var responsavelMae = new PessoaRequest(NomeMae.ToTitleCase(), new ContatoRequest(CelularMae, TipoContato.Celular)) + { + Enderecos = new List { enderecoAluno } + }; + responsaveis.Add(new PessoaResponsavelRequest( + pessoaResponsavelId: PessoaResponsavelId.OrZero(), + responsavelId: ResponsavelId.OrZero(), + responsavelMae, + alunoId: AlunoId.OrZero(), + tipoResponsavel: TipoResponsavel.Mae)); + + if (NomePai is not null) + { + var responsavelPai = new PessoaRequest(NomePai.ToTitleCase(), new ContatoRequest(CelularPai, TipoContato.Celular)) + { + Enderecos = new List { enderecoAluno } + }; + responsaveis.Add(new PessoaResponsavelRequest( + pessoaResponsavelId: PessoaResponsavelId.OrZero(), + responsavelId: ResponsavelId.OrZero(), + responsavelPai, + alunoId: AlunoId.OrZero(), + tipoResponsavel: TipoResponsavel.Pai)); + } + + return responsaveis; + } + private void CheckFormIsValid() { IsValid = string.IsNullOrWhiteSpace(Nome).Not() @@ -432,13 +457,7 @@ public override async Task Appearing(object args) }); if (Turmas.Count == 1) - { TurmaSelecionada = Turmas[0]; - } - else if (TurmaSelecionada is not null) - { - - } } catch (Exception ex) { @@ -461,5 +480,16 @@ public override async Task Appearing(object args) IsBusy = false; } } + + private void TurmaAlterada(TurmaResponse turmaSelecionadaAnteriormente) + { + if (TurmaSelecionada is not null && TurmaSelecionada.Equals(turmaSelecionadaAnteriormente).Not()) + { + MainThread.BeginInvokeOnMainThread(() => + { + DataNascimento = DateTime.Now.AddYears(-TurmaSelecionada.IdadeMaxima); + }); + } + } } }