Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions HomeCinema.Data/Abstract/IEntityBaseRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Abstract
{
//public interface IEntityBaseRepository { }

public interface IEntityBaseRepository<T> where T : class, IEntityBase, new()
{
IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
IQueryable<T> All { get; }
IQueryable<T> GetAll();
int Count();
T GetSingle(int id);
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties);
T GetSingle(Expression<Func<T, bool>> predicate);
T GetById(object id);

bool Any(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Delete(IEnumerable<T> entities);

void Edit(T entity);
void DeleteWhere(Expression<Func<T, bool>> predicate);
void Commit();
}
}
15 changes: 15 additions & 0 deletions HomeCinema.Data/Abstract/IRepositories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Abstract
{
public interface ICustomerRepository : IEntityBaseRepository<Customer> { }

//public interface IUserRepository : IEntityBaseRepository<User> { }

//public interface IAttendeeRepository : IEntityBaseRepository<Attendee> { }
}
18 changes: 18 additions & 0 deletions HomeCinema.Data/Configurations/ActivityLogConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Configurations
{
public class ActivityLogConfiguration : EntityBaseConfiguration<ActivityLog>
{
public ActivityLogConfiguration()
{
//Property(g => g.).IsRequired().HasMaxLength(50);
}
}
}
1 change: 1 addition & 0 deletions HomeCinema.Data/Extensions/CustomerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HomeCinema.Data.Repositories;
using HomeCinema.Data.Abstract;
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
Expand Down
62 changes: 31 additions & 31 deletions HomeCinema.Data/Extensions/StockExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using HomeCinema.Data.Repositories;
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Extensions
{
public static class StockExtensions
{
public static IEnumerable<Stock> GetAvailableItems(this IEntityBaseRepository<Stock> stocksRepository, int movieId)
{
IEnumerable<Stock> _availableItems;

_availableItems = stocksRepository.GetAll().Where(s => s.MovieId == movieId && s.IsAvailable);

return _availableItems;
}

//public static IEnumerable<Stock> GetAllItems(this IEntityBaseRepository<Stock> stocksRepository, int movieId)
//{
// IEnumerable<Stock> _allItems;

// _allItems = stocksRepository.GetAll().Where(s => s.MovieId == movieId);

// return _allItems;
//}
}
}
using HomeCinema.Data.Abstract;
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeCinema.Data.Extensions
{
public static class StockExtensions
{
public static IEnumerable<Stock> GetAvailableItems(this IEntityBaseRepository<Stock> stocksRepository, int movieId)
{
IEnumerable<Stock> _availableItems;
_availableItems = stocksRepository.GetAll().Where(s => s.MovieId == movieId && s.IsAvailable);
return _availableItems;
}
//public static IEnumerable<Stock> GetAllItems(this IEntityBaseRepository<Stock> stocksRepository, int movieId)
//{
// IEnumerable<Stock> _allItems;
// _allItems = stocksRepository.GetAll().Where(s => s.MovieId == movieId);
// return _allItems;
//}
}
}
37 changes: 19 additions & 18 deletions HomeCinema.Data/Extensions/UserExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using HomeCinema.Data.Repositories;
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Extensions
{
public static class UserExtensions
{
public static User GetSingleByUsername(this IEntityBaseRepository<User> userRepository, string username)
{
return userRepository.GetAll().FirstOrDefault(x => x.Username == username);
}
}
}
using HomeCinema.Data.Abstract;
using HomeCinema.Data.Repositories;
using HomeCinema.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Extensions
{
public static class UserExtensions
{
public static User GetSingleByUsername(this IEntityBaseRepository<User> userRepository, string username)
{
return userRepository.GetAll().FirstOrDefault(x => x.Username == username);
}
}
}
82 changes: 82 additions & 0 deletions HomeCinema.Data/Helper/EntityHelperMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Helper
{
public static class EntityHelperMethods
{
public static IQueryable<TEntity> WhereAllPropertiesOfSimilarTypeAreEqual<TEntity, TProperty>(this IQueryable<TEntity> query, TProperty value)
{
var param = Expression.Parameter(typeof(TEntity));

var predicate = PredicateBuilder.False<TEntity>();

foreach (var fieldName in GetEntityFieldsToCompareTo<TEntity, TProperty>())
{
var predicateToAdd = Expression.Lambda<Func<TEntity, bool>>(
Expression.Equal(
Expression.PropertyOrField(param, fieldName.Key),
Expression.Constant(value)), param);

predicate = predicate.Or(predicateToAdd);
}

return query.Where(predicate);
}

public static IQueryable<TEntity> WhereAllPropertiesOfSimilarTypeContains<TEntity, TProperty>(this IQueryable<TEntity> query, TProperty value)
{
var param = Expression.Parameter(typeof(TEntity));
var predicate = PredicateBuilder.False<TEntity>();
MethodInfo contains = typeof(string).GetMethod("Contains");
foreach (var fieldName in GetEntityFieldsToCompareTo<TEntity, TProperty>())
{
if (fieldName.Value.Equals(typeof(string).Name))
{
var predicateToAdd = Expression.Lambda<Func<TEntity, bool>>(
Expression.Call(Expression.PropertyOrField(param, fieldName.Key),
contains,
Expression.Constant(value)), param);
predicate = predicate.Or(predicateToAdd);
}
else
{
var predicateToAdd = Expression.Lambda<Func<TEntity, bool>>(
Expression.Equal(
Expression.PropertyOrField(param, fieldName.Key),
Expression.Constant(value)), param);
predicate = predicate.Or(predicateToAdd);
}

}

return query.Where(predicate);
}


// TODO: You'll need to find out what fields are actually ones you would want to compare on.
// This might involve stripping out properties marked with [NotMapped] attributes, for
// for example.
private static IDictionary<string, string> GetEntityFieldsToCompareTo<TEntity, TProperty>()
{
Type entityType = typeof(TEntity);
Type propertyType = typeof(TProperty);

var fields = entityType.GetFields()
.Where(f => f.FieldType == propertyType)
.ToDictionary(f => f.Name, f => f.FieldType.Name);

var properties = entityType.GetProperties()
.Where(p => p.PropertyType == propertyType)
.ToDictionary(p => p.Name, p => p.PropertyType.Name);
//return null;
Dictionary<string, string> d3 = fields.Concat(properties).ToDictionary(e => e.Key, e => e.Value);
return d3;
}
}
}
96 changes: 96 additions & 0 deletions HomeCinema.Data/Helper/PredicateBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HomeCinema.Data.Helper
{
public static class PredicateBuilder
{
/// <summary>
/// Creates a predicate that evaluates to true.
/// </summary>
public static Expression<Func<T, bool>> True<T>() { return param => true; }

/// <summary>
/// Creates a predicate that evaluates to false.
/// </summary>
public static Expression<Func<T, bool>> False<T>() { return param => false; }

/// <summary>
/// Creates a predicate expression from the specified lambda expression.
/// </summary>
public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; }

/// <summary>
/// Combines the first predicate with the second using the logical "and".
/// </summary>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}

/// <summary>
/// Combines the first predicate with the second using the logical "or".
/// </summary>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}

/// <summary>
/// Negates the predicate.
/// </summary>
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
{
var negated = Expression.Not(expression.Body);
return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
}

/// <summary>
/// Combines the first expression with the second using the specified merge function.
/// </summary>
static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
// zip parameters (map from parameters of second to parameters of first)
var map = first.Parameters
.Select((f, i) => new { f, s = second.Parameters[i] })
.ToDictionary(p => p.s, p => p.f);

// replace parameters in the second lambda expression with the parameters in the first
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

// create a merged lambda expression with parameters from the first expression
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}

class ParameterRebinder : ExpressionVisitor
{
readonly Dictionary<ParameterExpression, ParameterExpression> map;

ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}

public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}

protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;

if (map.TryGetValue(p, out replacement))
{
p = replacement;
}

return base.VisitParameter(p);
}
}
}
}
Loading