Skip to content

Commit

Permalink
export procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Carlos Moreira da Silva committed Jul 29, 2018
1 parent bd1703f commit b1f5c08
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 61 deletions.
32 changes: 32 additions & 0 deletions Banco de Dados/Procedures/p_sel_pets_not_adopted.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Jean Moreira
-- Create date: 29/07/2018
-- Description: Este procedimento tem como finalidade consultar os animais que ainda não foram adotados.
-- =============================================
CREATE PROCEDURE p_sel_pets_not_adopted
AS
BEGIN
SET NOCOUNT ON;

SELECT

[cd_animal] as Id,
[nm_animal] as Name,
[dc_animal] as Description,
[animal].[cd_tipo_animal] as TypePetId,
[T].[cd_tipo_animal] as split,
[T].[cd_tipo_animal] as Id,
[T].[dc_tipo_animal] as Description

FROM [dbo].[animal]

INNER JOIN [dbo].[tipo_animal] as [T] on [T].[cd_tipo_animal] = [animal].[cd_tipo_animal]

WHERE [cd_animal] NOT IN(SELECT cd_animal FROM adocao)

END
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Jean Moreira
-- Create date: 29/07/2018
-- Description: Este procedimento tem como finalidade consultar os responsáveis e suas adoções pelo código do responsável.
-- =============================================
ALTER PROCEDURE p_sel_reponsable_and_their_adoptions_by_id
@cd_responsavel uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;

SELECT
(SELECT COUNT([cd_responsavel])
FROM [dbo].[adocao] as [A] WHERE [R].[cd_responsavel] = [A].[cd_responsavel]) as AdoptedPets
,[cd_responsavel] as split
,[cd_responsavel] as Id
,[nm_responsavel] as Name
,0 as splitCpf
,[cpf_responsavel] as Number
,0 as splitEmail
,[email_responsavel] as Address

FROM [dbo].[responsavel] as [R]
WHERE [R].[cd_responsavel] = @cd_responsavel
END
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Jean Moreira
-- Create date: 29/07/2018
-- Description: Este procedimento tem como finalidade consultar os responsáveis e suas adoções
-- =============================================
CREATE PROCEDURE p_sel_reponsables_and_their_adoptions

AS
BEGIN
SET NOCOUNT ON;

SELECT
(SELECT COUNT([cd_responsavel])
FROM [dbo].[adocao] as [A] WHERE [R].[cd_responsavel] = [A].[cd_responsavel]) as AdoptedPets
,[cd_responsavel] as split
,[cd_responsavel] as Id
,[nm_responsavel] as Name
,0 as splitCpf
,[cpf_responsavel] as Number
,0 as splitEmail
,[email_responsavel] as Address

FROM [dbo].[responsavel] as [R]

END
GO
17 changes: 17 additions & 0 deletions Banco de Dados/Procedures/p_sel_type_pets.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Jean Moreira
-- Create date: 29/07/2018
-- Description: Este procedimento tem como finalidade consultar os tipos dos animais.
-- =============================================
CREATE PROCEDURE p_sel_type_pets
AS
BEGIN
SET NOCOUNT ON;
SELECT [cd_tipo_animal] as Id, [dc_tipo_animal] as Description FROM [dbo].[tipo_animal]

END
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace SGA.Infra.Dapper.Core
{
public class BaseQuery<T> : IQuery<T> where T : class
public class Query<T> : IQuery<T> where T : class
{
protected readonly IConnectionFactory ConnectionFactory;

public BaseQuery(IConnectionFactory connection)
public Query(IConnectionFactory connection)
{
ConnectionFactory = connection;
}
Expand Down
24 changes: 24 additions & 0 deletions Código Fonte/SGA/SGA.Infra.Dapper/Maps/AdoptionMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using SGA.Application.Domain.Dtos;
using SGA.Domain.Entities.Models;
using SGA.Domain.Entities.ValueObjects;
using System;

namespace SGA.Infra.Dapper.Maps
{
public static class AdoptionMap
{
public static Func<AdoptionDto, Responsible, Cpf, Email, AdoptionDto> AdoptionDto()
{
return (ad, res, cpf, email) =>
{
res.SetCpf(cpf);
res.SetEmail(email);
ad.Responsible = res;
return ad;
};
}
}
}
18 changes: 18 additions & 0 deletions Código Fonte/SGA/SGA.Infra.Dapper/Maps/PetMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using SGA.Domain.Entities.Models;
using System;

namespace SGA.Infra.Dapper.Maps
{
public static class PetMap
{
public static Func<Pet, TypePet, Pet> FullPet()
{
return (p, t) =>
{
p.SetTypePet(t);
return p;
};
}
}
}
24 changes: 3 additions & 21 deletions Código Fonte/SGA/SGA.Infra.Dapper/Procedures/AdoptionProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,9 @@
{
public static class AdoptionProcedure
{
public const string GetReponsablesAndTheirAdoptions =
" SELECT(SELECT COUNT(cd_responsavel) FROM adocao WHERE responsavel.cd_responsavel = adocao.cd_responsavel) as AdoptedPets"
+ " ,cd_responsavel as split"
+ " ,cd_responsavel as Id"
+ " ,nm_responsavel as Name"
+ " ,0 as splitCpf"
+ " ,cpf_responsavel as Number"
+ " ,0 as splitEmail"
+ " ,email_responsavel as Address"
+ " FROM responsavel ";
public const string GetReponsablesAndTheirAdoptions = "p_sel_reponsables_and_their_adoptions";

public const string FindReponsableAndTheirAdoptionsById =
" SELECT(SELECT COUNT(cd_responsavel) FROM adocao WHERE responsavel.cd_responsavel = adocao.cd_responsavel) as AdoptedPets"
+ " ,cd_responsavel as split"
+ " ,cd_responsavel as Id"
+ " ,nm_responsavel as Name"
+ " ,0 as splitCpf"
+ " ,cpf_responsavel as Number"
+ " ,0 as splitEmail"
+ " ,email_responsavel as Address"
+ " FROM responsavel "
+ " WHERE responsavel.cd_responsavel = '{0}' ";

public const string FindReponsableAndTheirAdoptionsById = "p_sel_reponsable_and_their_adoptions_by_id";
}
}
9 changes: 2 additions & 7 deletions Código Fonte/SGA/SGA.Infra.Dapper/Procedures/PetProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
{
public static class PetProcedure
{
public const string GetTypePets =
"SELECT [cd_tipo_animal] as Id, [dc_tipo_animal] as Description FROM[SGA].[dbo].[tipo_animal]";
public const string GetTypePets = "p_sel_type_pets";

public const string GetPetsNotAdopted =
" SELECT[cd_animal] as Id, [nm_animal] as Name, [dc_animal] as Description, [animal].[cd_tipo_animal] as TypePetId, " +
" t.[cd_tipo_animal] as split, t.[cd_tipo_animal] as Id, t.[dc_tipo_animal] as Description " +
" FROM[SGA].[dbo].[animal] INNER JOIN [dbo].[tipo_animal] as t on t.cd_tipo_animal = [animal].[cd_tipo_animal] " +
" WHERE [cd_animal] NOT IN(SELECT cd_animal FROM adocao)";
public const string GetPetsNotAdopted = "p_sel_pets_not_adopted";
}
}
38 changes: 18 additions & 20 deletions Código Fonte/SGA/SGA.Infra.Dapper/Queries/AdoptionQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
using SGA.Application.Domain.Dtos;
using SGA.Application.Domain.Queries;
using SGA.Domain.Entities.Models;
using SGA.Domain.Entities.ValueObjects;
using SGA.Infra.Dapper.Core;
using SGA.Infra.Dapper.Maps;
using SGA.Infra.Dapper.Procedures;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace SGA.Infra.Dapper.Queries
{
public class AdoptionQuery : BaseQuery<Adoption>, IAdoptionQuery
public class AdoptionQuery : Query<Adoption>, IAdoptionQuery
{
public AdoptionQuery(IConnectionFactory connection) : base(connection)
{
Expand All @@ -24,37 +25,34 @@ public IEnumerable<AdoptionDto> GetReponsablesAndTheirAdoptions()

using (var connection = ConnectionFactory.GetConnection())
{
adoptions = connection.Query<AdoptionDto, Responsible, Cpf, Email, AdoptionDto>(
adoptions = connection.Query(
AdoptionProcedure.GetReponsablesAndTheirAdoptions,
(ad, res, cpf, email) =>
{
res.SetCpf(cpf);
res.SetEmail(email);
ad.Responsible = res;
return ad;
}, splitOn: "split, splitCpf, splitEmail");
AdoptionMap.AdoptionDto(),
splitOn: "split, splitCpf, splitEmail",
commandType: CommandType.StoredProcedure
).AsQueryable();
}

return adoptions;
}

public AdoptionDto FindReponsableAndTheirAdoptionsBy(Guid id)
{
var sql = string.Format(AdoptionProcedure.FindReponsableAndTheirAdoptionsById, id);
var parameters = new DynamicParameters();

parameters.Add("@cd_responsavel", id);

AdoptionDto adoptionDto;

using (var connection = ConnectionFactory.GetConnection())
{
adoptionDto = connection.Query<AdoptionDto, Responsible, Cpf, Email, AdoptionDto>(
sql,
(ad, res, cpf, email) =>
{
res.SetCpf(cpf);
res.SetEmail(email);
ad.Responsible = res;
return ad;
}, splitOn: "split, splitCpf, splitEmail").SingleOrDefault();
adoptionDto = connection.Query(
AdoptionProcedure.FindReponsableAndTheirAdoptionsById,
AdoptionMap.AdoptionDto(),
parameters,
splitOn: "split, splitCpf, splitEmail",
commandType: CommandType.StoredProcedure
).SingleOrDefault();
}

return adoptionDto;
Expand Down
22 changes: 12 additions & 10 deletions Código Fonte/SGA/SGA.Infra.Dapper/Queries/PetQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
using SGA.Application.Domain.Queries;
using SGA.Domain.Entities.Models;
using SGA.Infra.Dapper.Core;
using SGA.Infra.Dapper.Maps;
using SGA.Infra.Dapper.Procedures;
using System.Collections.Generic;
using System.Data;

namespace SGA.Infra.Dapper.Queries
{
public class PetQuery : BaseQuery<Pet>, IPetQuery
public class PetQuery : Query<Pet>, IPetQuery
{
public PetQuery(IConnectionFactory connection) : base(connection)
{
}

public IEnumerable<TypePet> GetTypePets()
{
IEnumerable<TypePet> entities;
IEnumerable<TypePet> typesPets;

using (var connection = ConnectionFactory.GetConnection())
{
entities = connection.Query<TypePet>(PetProcedure.GetTypePets);
typesPets = connection.Query<TypePet>(
PetProcedure.GetTypePets,
commandType: CommandType.StoredProcedure);
}

return entities;
return typesPets;
}

public IEnumerable<Pet> GetPetsNotAdopted()
Expand All @@ -32,13 +36,11 @@ public IEnumerable<Pet> GetPetsNotAdopted()

using (var connection = ConnectionFactory.GetConnection())
{
pets = connection.Query<Pet, TypePet, Pet>(
pets = connection.Query(
PetProcedure.GetPetsNotAdopted,
(p, t) =>
{
p.SetTypePet(t);
return p;
}, splitOn: "split");
PetMap.FullPet(),
splitOn: "split",
commandType: CommandType.StoredProcedure);
}

return pets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace SGA.Infra.Dapper.Queries
{
public class ResponsibleQuery : BaseQuery<Responsible>, IResponsibleQuery
public class ResponsibleQuery : Query<Responsible>, IResponsibleQuery
{
public ResponsibleQuery(IConnectionFactory connection) : base(connection)
{
Expand Down

0 comments on commit b1f5c08

Please sign in to comment.