public void GetAuctions()
        {
            try
            {
                var options = CreateNewContextOptions();
                using (var db = new AuctionContext(options))
                {
                    var repository =
                        new AuctionRepository(db);
                    AuctionTestHelper.PopulateDefaultData(db);
                    var item  = GenerateModel();
                    var item2 = GenerateModel();
                    item2.Code = "test2";
                    var item3 = GenerateModel();
                    item3.Code = "test3";
                    var cats = db.Set <Category>().ToList();

                    Assert.DoesNotThrow(() => repository.Save(item));
                    Assert.DoesNotThrow(() => repository.Save(item2));
                    Assert.DoesNotThrow(() => repository.Save(item3));
                    foreach (var category in cats)
                    {
                        Assert.DoesNotThrow(() => repository.AddCategory(item, category));
                    }
                    Assert.DoesNotThrow(() => repository.AddCategory(item2, cats.First()));

                    var user = new User
                    {
                        Id = 1
                    };

                    Assert.DoesNotThrow(() => repository.MakeBid(user, item, 1000));
                    Assert.DoesNotThrow(() => repository.MakeBid(new User
                    {
                        Id = 2
                    }, item, 800));
                    Assert.Throws <InvalidOperationException>(() => repository.MakeBid(user, item, 300));
                }
                using (var db = new AuctionContext(options))
                {
                    var cats   = db.Set <Category>().ToList();
                    var filter = new AuctionFilter
                    {
                        UserId = 1
                    };
                    IEnumerable <odec.Server.Model.Auction.Auction> result = null;
                    var repository =
                        new AuctionRepository(db);
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.UserId = null;
                    filter.InitialPriceInterval = new Interval <decimal?> {
                        Start = 100
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.InitialPriceInterval.End  = 400;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.InitialPriceInterval.End  = 200;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result == null || !result.Any());
                    filter.InitialPriceInterval      = null;
                    filter.AuctionTypeId             = 1;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.AuctionTypeId             = null;
                    filter.Title                     = "My";
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.Title             = null;
                    filter.StartDateInterval = new Interval <DateTime?> {
                        Start = DateTime.Today.AddDays(-7)
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.StartDateInterval.End     = DateTime.Today;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.StartDateInterval = null;
                    filter.MaxPriceInterval  = new Interval <decimal?>
                    {
                        Start = 800
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.MaxPriceInterval.End      = 900;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result == null || !result.Any());
                    filter.MaxPriceInterval.End      = 1000;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.MaxPriceInterval = null;

                    filter.Categories = cats;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any() && result.Count() == 2);
                    filter.Categories                = null;
                    filter.UserId                    = 1;
                    filter.AuctionTypeId             = 1;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.Title = "my";
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.InitialPriceInterval = new Interval <decimal?>
                    {
                        Start = 100
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.InitialPriceInterval.End  = 400;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.StartDateInterval = new Interval <DateTime?>
                    {
                        Start = DateTime.Today.AddDays(-7)
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.StartDateInterval.End     = DateTime.Today;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.MaxPriceInterval = new Interval <decimal?> {
                        Start = 800
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.MaxPriceInterval.End      = 1100;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));
                    Assert.True(result != null && result.Any());
                    filter.Categories = cats;
                    Assert.DoesNotThrow(() => result = repository.Get(filter));

                    Assert.True(result != null && result.Any() && result.Count() == 1);
                    filter = new AuctionFilter
                    {
                        EndDateInterval = new Interval <DateTime?>()
                        {
                            // End = DateTime.Today,
                        },
                        StateId = 1,
                        //AuctionTypeId = (int)AuctionTypes.Crafter,
                        Rows = 1000,
                        InitialPriceInterval   = new Interval <decimal?>(),
                        MaxPriceInterval       = new Interval <decimal?>(),
                        Categories             = new List <Category>(),
                        StartDateInterval      = new Interval <DateTime?>(),
                        AutoClosePriceInterval = new Interval <decimal?>()
                    };
                    Assert.DoesNotThrow(() => result = repository.Get(filter));

                    Assert.True(result != null && result.Any() && result.Count() > 1);
                }
            }
            catch (Exception ex)
            {
                LogEventManager.Logger.Error(ex);
                throw;
            }
        }
Exemple #2
0
        public IEnumerable <Server.Model.Auction.Auction> Get(AuctionFilter filter)
        {
            try
            {
                var query = from auction in Db.Set <Server.Model.Auction.Auction>()
                            select auction;
                if (filter.UserId.HasValue)
                {
                    query = query.Where(it => it.UserId == filter.UserId);
                }

                if (filter.StateId.HasValue)
                {
                    query = query.Where(it => it.StateId == filter.StateId);
                }

                if (filter.Categories != null && filter.Categories.Any())
                {
                    var cIds = filter.Categories.Select(it => it.Id);
                    //Union
                    //TODO: Cross
                    query = from auction in query
                            join auctionSkill in Db.Set <AuctionSkill>() on auction.Id equals auctionSkill.AuctionId
                            where cIds.Contains(auctionSkill.CategoryId)
                            select auction;
                }

                if (filter.AuctionTypeId.HasValue)
                {
                    query = query.Where(it => it.AuctionTypeId == filter.AuctionTypeId);
                }

                if (filter.StartDateInterval?.Start != null)
                {
                    query = query.Where(it => it.StartDate >= filter.StartDateInterval.Start);
                }
                if (filter.StartDateInterval?.End != null)
                {
                    query = query.Where(it => it.StartDate <= filter.StartDateInterval.End);
                }
                if (filter.EndDateInterval?.Start != null)
                {
                    query = query.Where(it => it.EndDate >= filter.EndDateInterval.Start);
                }
                if (filter.EndDateInterval?.End != null)
                {
                    query = query.Where(it => it.EndDate <= filter.EndDateInterval.End);
                }

                if (filter.InitialPriceInterval != null && filter.InitialPriceInterval.Start.HasValue)
                {
                    query = query.Where(it => it.InitialPrice >= filter.InitialPriceInterval.Start);
                }
                if (filter.InitialPriceInterval != null && filter.InitialPriceInterval.End.HasValue)
                {
                    query = query.Where(it => it.InitialPrice <= filter.InitialPriceInterval.End);
                }

                if (filter.AutoClosePriceInterval != null && filter.AutoClosePriceInterval.Start.HasValue)
                {
                    query = query.Where(it => it.AutoClosePrice >= filter.AutoClosePriceInterval.Start);
                }
                if (filter.AutoClosePriceInterval != null && filter.AutoClosePriceInterval.End.HasValue)
                {
                    query = query.Where(it => it.AutoClosePrice <= filter.AutoClosePriceInterval.End);
                }
                //todo: check the start end too
                if (filter.MaxPriceInterval != null && (filter.MaxPriceInterval.Start.HasValue || filter.MaxPriceInterval.End.HasValue))
                {
                    //TODO: Id Don't like it. It should be refactored
                    var query2 = from auction in query
                                 join auctionBid in Db.Set <AuctionBid>() on auction.Id equals auctionBid.AuctionId
                                 group new { auctionBid } by
                    new { auctionBid.AuctionId }
                    into tmp
                    select tmp;
                    if (filter.MaxPriceInterval.Start.HasValue)
                    {
                        query2 = query2.Where(it => it.Max(it2 => it2.auctionBid.Value) >= filter.MaxPriceInterval.Start);
                    }
                    if (filter.MaxPriceInterval.End.HasValue)
                    {
                        query2 = query2.Where(it => it.Max(it2 => it2.auctionBid.Value) <= filter.MaxPriceInterval.End);
                    }

                    query = from auction in query
                            join auction2 in query2 on auction.Id equals auction2.Key.AuctionId
                            select auction;
                }
                //Case sensetive contains
                if (!string.IsNullOrEmpty(filter.Title))
                {
                    query = query.Where(it => it.Name.ToUpper().Contains(filter.Title.ToUpper()));
                }
                //todo:add filter bool flag to initialize includes
                if (true)
                {
                    query = query.Include(it => it.User).Include(it => it.AuctionType);
                }

                query = filter.Sord.Equals("desc", StringComparison.OrdinalIgnoreCase)
                    ? query.OrderByDescending(filter.Sidx)
                    : query.OrderBy(filter.Sidx);

                var result = query.Skip(filter.Rows * (filter.Page - 1)).Take(filter.Rows).Distinct();
                return(result);
            }
            catch (Exception ex)
            {
                LogEventManager.Logger.Error(ex.Message, ex);
                throw;
            }
        }