public void GetBattles_with_empty_model_succeeds()
        {
            using (var battleController = ResolveController<VideoBattleController>())
            {
                //save some battles first
                foreach(var battle in SampleVideoBattles.GetSampleVideoBattles())
                    SaveEntity(battle);

                var queryModel = new VideoBattleQueryModel();

                var response = battleController.GetBattles(queryModel);
                Assert.IsTrue(response.GetValue<bool>("Success"));

                var count = response.GetValue<dynamic>("ResponseData").VideoBattles.Count;
                Assert.AreEqual(SampleVideoBattles.GetSampleVideoBattles().Count, count);
            }
        }
        public IHttpActionResult GetBattles([FromUri] VideoBattleQueryModel requestModel)
        {
            if (requestModel == null)
            {
                //set default values
                requestModel = new VideoBattleQueryModel();
            }
            if (requestModel.Count == 0)
                requestModel.Count = 15;

            if (requestModel.Page <= 0)
                requestModel.Page = 1;

            if (!requestModel.BattlesSortBy.HasValue)
                requestModel.BattlesSortBy = BattlesSortBy.Id;

            if (!requestModel.SortOrder.HasValue)
                requestModel.SortOrder = SortOrder.Descending;

            if (string.IsNullOrEmpty(requestModel.ViewType))
                requestModel.ViewType = "open";

            //let's get all the battles depending on view type
            IList<VideoBattle> battles = null;
            int totalPages = 0;
            switch (requestModel.ViewType)
            {
                case "open":
                    battles = _videoBattleService.GetAll(null, null, null, BattleStatus.Open, null, null, string.Empty,
                        requestModel.BattlesSortBy, requestModel.SortOrder, out totalPages, requestModel.Page,
                        requestModel.Count);
                    battles = battles.ToList();
                    break;
                case "open-to-join":
                    battles = _videoBattleService.GetAll(null, null, null, BattleStatus.Pending,
                        BattleParticipationType.Open, null, string.Empty, requestModel.BattlesSortBy,
                        requestModel.SortOrder, out totalPages, requestModel.Page, requestModel.Count);
                    battles = battles.ToList();
                    break;
                case "challenged":
                    battles = _videoBattleService.GetAll(null, ApplicationContext.Current.CurrentUser.Id, null,
                        BattleStatus.Pending, null, null, string.Empty, requestModel.BattlesSortBy,
                        requestModel.SortOrder, out totalPages, requestModel.Page, requestModel.Count);
                    battles = battles.ToList();
                    break;
                case "closed":
                    //either closed or complete..whichever it is so first get all of them
                    battles = _videoBattleService.GetAll(null, null, null, null, null, null, string.Empty,
                        requestModel.BattlesSortBy, requestModel.SortOrder, out totalPages, 1, int.MaxValue);

                    battles =
                        battles.Where(
                            x =>
                                x.VideoBattleStatus == BattleStatus.Closed ||
                                x.VideoBattleStatus == BattleStatus.Complete)
                            .ToList();
                    totalPages = int.Parse(Math.Ceiling((decimal)battles.Count() / requestModel.Count).ToString());

                    battles = battles.Skip((requestModel.Page - 1) * requestModel.Count)
                        .Take(requestModel.Count).ToList();

                    break;
                case "my":
                    battles = _videoBattleService.GetAll(ApplicationContext.Current.CurrentUser.Id, null, null, null,
                        null, null, string.Empty, requestModel.BattlesSortBy, requestModel.SortOrder, out totalPages,
                        requestModel.Page, requestModel.Count);
                    battles = battles.ToList();
                    break;
                case "search":
                    battles = _videoBattleService.GetAll(null, null, null, null, BattleParticipationType.Open, null,
                        requestModel.SearchTerm, requestModel.BattlesSortBy, requestModel.SortOrder, out totalPages,
                        requestModel.Page, requestModel.Count);
                    battles = battles.ToList();
                    break;
                case "sponsor":
                    battles = _videoBattleService.GetAll(ApplicationContext.Current.CurrentUser.Id, null, null, null,
                        null, true, string.Empty, requestModel.BattlesSortBy, requestModel.SortOrder, out totalPages,
                        requestModel.Page, requestModel.Count);
                    battles = battles.ToList();
                    break;
                case "user":
                    if (requestModel.UserId == 0)
                        requestModel.UserId = ApplicationContext.Current.CurrentUser.Id;
                    battles = _videoBattleService.GetAll(requestModel.UserId, null, null, null, null, true,
                        string.Empty, requestModel.BattlesSortBy, requestModel.SortOrder, out totalPages,
                        requestModel.Page, requestModel.Count);
                    battles = battles.ToList();
                    break;
            }

            var model = new List<VideoBattlePublicModel>();
            if (battles != null)
            {
                foreach (var videoBattle in battles)
                {
                    //get the owner of battle
                    var challenger = _userService.Get(videoBattle.ChallengerId);
                    if (challenger == null)
                        continue;

                    var battleVideos = _videoBattleVideoService.GetBattleVideos(videoBattle.Id);

                    var thumbnailVideo = battleVideos.FirstOrDefault(x => !string.IsNullOrEmpty(x.ThumbnailPath));

                    var thumbnailUrl = _battleSettings.DefaultVideosFeaturedImageUrl;
                    if (thumbnailVideo != null && videoBattle.VideoBattleStatus != BattleStatus.Pending)
                        thumbnailUrl = thumbnailVideo.ThumbnailPath;

                    //and relative path to url
                    thumbnailUrl = WebHelper.GetUrlFromPath(thumbnailUrl, _generalSettings.ApplicationUiDomain);

                    //prizes
                    var allPrizes = _videoBattlePrizeService.GetBattlePrizes(videoBattle.Id);

                    model.Add(new VideoBattlePublicModel() {
                        Name = videoBattle.Name,
                        Description = videoBattle.Description,
                        VotingStartDate = videoBattle.VotingStartDate,
                        VotingEndDate = videoBattle.VotingEndDate,
                        DateCreated = videoBattle.DateCreated,
                        DateUpdated = videoBattle.DateUpdated,
                        VideoBattleStatus = videoBattle.VideoBattleStatus,
                        BattleParticipationType = videoBattle.ParticipationType,
                        VideoBattleVoteType = videoBattle.VideoBattleVoteType,
                        Id = videoBattle.Id,
                        IsEditable = CanEdit(videoBattle),
                        ChallengerName = challenger.GetPropertyValueAs<string>(PropertyNames.DisplayName),
                        ChallengerSeName =
                            Url.Route("CustomerProfileUrl",
                                new RouteValueDictionary() { { "SeName", challenger.GetPermalink() } }),
                        VideoBattleSeName =
                            Url.Route("VideoBattlePage",
                                new RouteValueDictionary() { { "SeName", videoBattle.GetPermalink() } }),
                        RemainingSeconds = videoBattle.GetRemainingSeconds(),
                        VideoBattleFeaturedImageUrl = thumbnailUrl,
                        ChallengerProfileImageUrl =
                            _pictureService.GetPictureUrl(
                                challenger.GetPropertyValueAs<int>(PropertyNames.DefaultPictureId)),
                        IsSponsorshipSupported = videoBattle.IsSponsorshipSupported,
                        MinimumSponsorshipAmount = videoBattle.MinimumSponsorshipAmount,
                        ConsolidatedPrizesDisplay = videoBattle.GetConsolidatedPrizesString(allPrizes.ToList(), null, _sponsorService, _settingService, _paymentProcessingService, _formatterService, _creditService, _battleSettings)
                    });
                }
            }
            return RespondSuccess(new {
                VideoBattles = model,
                TotalPages = totalPages
            });
        }