Skip to content

Commit

Permalink
Adicionando migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ozielguimaraes committed Nov 27, 2023
1 parent 2688b93 commit cf2e20e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public class DateTimeToDateOnlyConverter : JsonConverter<DateOnly>
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");
}
Expand All @@ -22,4 +24,5 @@ public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializer
writer.WriteStringValue(value.ToString());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Ebd.Infra.Data.Migrations
{
/// <inheritdoc />
public partial class DataNascimento : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateOnly>(
name: "NascidoEm",
table: "Pessoa",
type: "date",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateTime>(
name: "NascidoEm",
table: "Pessoa",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateOnly),
oldType: "date");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("PessoaId"));

b.Property<DateTime>("NascidoEm")
.HasColumnType("timestamp with time zone");
b.Property<DateOnly>("NascidoEm")
.HasColumnType("date");

b.Property<string>("Nome")
.IsRequired()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,13 +11,13 @@ public PessoaRequest(string nome, ContatoRequest contato)
{
Nome = nome;
Contatos = new List<ContatoRequest> { 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<EnderecoRequest> Enderecos { get; set; }
public ICollection<ContatoRequest> Contatos { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -148,7 +143,7 @@ public string Email
get => email;
set
{
SetProperty(ref email, value);
SetProperty(ref email, value?.ToLower());
CheckFormIsValid();
}
}
Expand All @@ -159,7 +154,7 @@ public string NomeMae
get => nomeMae;
set
{
SetProperty(ref nomeMae, value);
SetProperty(ref nomeMae, value?.ToTitleCase());
CheckFormIsValid();
}
}
Expand All @@ -181,7 +176,7 @@ public string NomePai
get => nomePai;
set
{
SetProperty(ref nomePai, value);
SetProperty(ref nomePai, value?.ToTitleCase());
CheckFormIsValid();
}
}
Expand Down Expand Up @@ -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<EnderecoRequest> { enderecoAluno }
};

var responsaveis = new List<PessoaResponsavelRequest>
{
Expand Down Expand Up @@ -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,
Expand All @@ -319,6 +310,7 @@ private async Task SalvarCommandExecute()
},
Enderecos = new List<EnderecoRequest> { enderecoAluno },
Responsaveis = responsaveis
Responsaveis = ObterResponsaveis(enderecoAluno)
};
var response = await _alunoService.SalvarAsync(alunoRequest);
if (response.IsSuccess)
Expand All @@ -341,6 +333,39 @@ private async Task SalvarCommandExecute()
throw;
}
}

private List<PessoaResponsavelRequest> ObterResponsaveis(EnderecoRequest enderecoAluno)
{
var responsaveis = new List<PessoaResponsavelRequest>();

var responsavelMae = new PessoaRequest(NomeMae.ToTitleCase(), new ContatoRequest(CelularMae, TipoContato.Celular))
{
Enderecos = new List<EnderecoRequest> { 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<EnderecoRequest> { 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()
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
});
}
}
}
}

0 comments on commit cf2e20e

Please sign in to comment.