summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/ParceiroRepository.cs
blob: 476ec1a461a1885c1106a76a69e408c509b4e443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using AutoMapper;
using Gestor.Infrastructure.Entities.Generic;
using Gestor.Infrastructure.Entities.Seguros;
using Gestor.Infrastructure.Mappers;
using Gestor.Infrastructure.Repository.Generic;
using Gestor.Infrastructure.Repository.Interface;
using Gestor.Infrastructure.UnitOfWork.Generic;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Seguros;
using NHibernate;
using NHibernate.Connection;
using NHibernate.Impl;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Gestor.Infrastructure.Repository.Logic
{
	public class ParceiroRepository : GenericRepository<ParceiroDb>, IParceiroRepository, IGenericRepository<ParceiroDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

		public ParceiroRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
		{
			this._unitOfWork = unitOfWork;
		}

		public void Delete(long id)
		{
			base.Delete(base.FindEntityById(id));
		}

		public List<Parceiro> Find()
		{
			List<ParceiroDb> list = base.All().ToList<ParceiroDb>();
			return ApplicationMapper.Mapper.Map<List<ParceiroDb>, List<Parceiro>>(list);
		}

		public List<Parceiro> Find(string filter)
		{
			ParceiroRepository.u003cu003ec__DisplayClass7_0 variable = null;
			IQueryable<ParceiroDb> parceiroDbs = base.All();
			ParameterExpression parameterExpression = Expression.Parameter(typeof(ParceiroDb), "x");
			IQueryable<ParceiroDb> parceiroDbs1 = parceiroDbs.Where<ParceiroDb>(Expression.Lambda<Func<ParceiroDb, bool>>(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(ParceiroDb).GetMethod("get_Nome").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty<Expression>()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(ParceiroRepository.u003cu003ec__DisplayClass7_0)), FieldInfo.GetFieldFromHandle(typeof(ParceiroRepository.u003cu003ec__DisplayClass7_0).GetField("filter").FieldHandle)) }), new ParameterExpression[] { parameterExpression }));
			parameterExpression = Expression.Parameter(typeof(ParceiroDb), "x");
			return parceiroDbs1.Select<ParceiroDb, Parceiro>(Expression.Lambda<Func<ParceiroDb, Parceiro>>(Expression.MemberInit(Expression.New(typeof(Parceiro)), new MemberBinding[] { Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(DomainBase).GetMethod("set_Id", new Type[] { typeof(long) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EntityBase).GetMethod("get_Id").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Parceiro).GetMethod("set_Nome", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(ParceiroDb).GetMethod("get_Nome").MethodHandle))) }), new ParameterExpression[] { parameterExpression })).ToList<Parceiro>();
		}

		public Parceiro FindById(long id)
		{
			ParceiroDb parceiroDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<ParceiroDb, Parceiro>(parceiroDb);
		}

		public long FindLastId()
		{
			long num;
			object connection;
			SqlCommand sqlCommand;
			SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
			if (sessionFactory != null)
			{
				connection = sessionFactory.ConnectionProvider.GetConnection();
			}
			else
			{
				connection = null;
			}
			using (SqlConnection sqlConnection = connection as SqlConnection)
			{
				if (sqlConnection != null)
				{
					sqlCommand = sqlConnection.CreateCommand();
				}
				else
				{
					sqlCommand = null;
				}
				using (SqlCommand sqlCommand1 = sqlCommand)
				{
					sqlCommand1.CommandText = "SELECT MAX(IDPARCEIRO) as id FROM parceiro";
					SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
					sqlDataReader.Read();
					num = (sqlDataReader["id"] == null ? (long)0 : long.Parse(sqlDataReader["id"].ToString()));
				}
			}
			return num;
		}

		public Parceiro Merge(Parceiro parceiro)
		{
			ParceiroDb parceiroDb = ApplicationMapper.Mapper.Map<Parceiro, ParceiroDb>(parceiro);
			base.Merge(parceiroDb);
			return ApplicationMapper.Mapper.Map<ParceiroDb, Parceiro>(parceiroDb);
		}

		public Parceiro SaveOrUpdate(Parceiro parceiro)
		{
			ParceiroDb parceiroDb = ApplicationMapper.Mapper.Map<Parceiro, ParceiroDb>(parceiro);
			this.SaveOrUpdate(parceiroDb);
			return ApplicationMapper.Mapper.Map<ParceiroDb, Parceiro>(parceiroDb);
		}
	}
}