public async Task <HttpResponseMessage> Get(Guid id) { var token = TokenExtracton.GetTokenFromCookie(HttpContext.Current.Request); using (var repository = new Repository(token)) { await repository.Context.Factions.LoadAsync(); await repository.Context.CardTypes.LoadAsync(); var result = await repository.Context.Decks.Include(x => x.DeckCards.Select(dc => dc.Card)).Where(x => x.Guid == id).FirstOrDefaultAsync(); if (result == null) { Request.CreateResponse(HttpStatusCode.NotFound); } var deck = result.FromDal(true); if (repository.ServiceUser != null) { await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); await repository.Context.Entry(result).Reference(x => x.Creator).LoadAsync(); deck.IsEditable = repository.ServiceUser.Role.Guid == PredefinedGuids.Administrator || repository.ServiceUser.Guid == result.Creator.Guid; } return(Request.CreateResponse(deck)); } }
public async Task <HttpResponseMessage> Post([FromBody] CardSearchOptions searchOptionsBase) { var token = TokenExtracton.GetTokenFromCookie(HttpContext.Current.Request); using (var repository = new Repository(token)) { IQueryable <CardModel> dbResult = repository.Context.Cards.Include(x => x.RuleSet).Include(x => x.Serie).Include(x => x.Faction).Include(x => x.Status).Include(x => x.Type).Include(x => x.Creator).Include(x => x.LastModifiedBy).AsNoTracking(); if (!searchOptionsBase.ShowDraftVersions) { dbResult = dbResult.Where(x => x.Status.Guid == PredefinedGuids.Final); } if (!string.IsNullOrWhiteSpace(searchOptionsBase.Search)) { dbResult = dbResult.Where(it => it.Name.Contains(searchOptionsBase.Search) || it.Creator.Name.Contains(searchOptionsBase.Search)); } var totalCount = dbResult.Count(); // default order by if (string.IsNullOrWhiteSpace(searchOptionsBase.OrderBy)) { searchOptionsBase.OrderBy = "Name"; } var orderByType = QueryHelper.GetPropertyType <CardModel>(searchOptionsBase.OrderBy); if (orderByType != null) { if (orderByType == typeof(string)) { var orderByExpression = QueryHelper.GetPropertyExpression <CardModel, string>(searchOptionsBase.OrderBy); dbResult = searchOptionsBase.ReverseOrder ? dbResult.OrderByDescending(orderByExpression) : dbResult.OrderBy(orderByExpression); } if (orderByType == typeof(int)) { var orderByExpression = QueryHelper.GetPropertyExpression <CardModel, int>(searchOptionsBase.OrderBy); dbResult = searchOptionsBase.ReverseOrder ? dbResult.OrderByDescending(orderByExpression) : dbResult.OrderBy(orderByExpression); } if (orderByType == typeof(DateTime)) { var orderByExpression = QueryHelper.GetPropertyExpression <CardModel, DateTime>(searchOptionsBase.OrderBy); dbResult = searchOptionsBase.ReverseOrder ? dbResult.OrderByDescending(orderByExpression) : dbResult.OrderBy(orderByExpression); } } searchOptionsBase.PageSize = Math.Min(50, searchOptionsBase.PageSize); var query = await dbResult.Skip((searchOptionsBase.PageNumber - 1) *searchOptionsBase.PageSize).Take(searchOptionsBase.PageSize).ToListAsync(); var result = new ResultList <Card>(query.Select(x => x.FromDal()).ToList()) { TotalItems = totalCount, SearchOptions = searchOptionsBase }; return(Request.CreateResponse(result)); } }
public async Task <HttpResponseMessage> Get() { var token = TokenExtracton.GetTokenFromCookie(HttpContext.Current.Request); using (var repository = new Repository(token)) { var decks = repository.Context.Decks.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); var result = new ResultList <Deck>() { TotalItems = decks.Count(), Items = decks, }; return(Request.CreateResponse(result)); } }
public HttpResponseMessage Get() { var token = TokenExtracton.GetTokenFromCookie(HttpContext.Current.Request); using (var repository = new Repository(token)) { var cardOptions = new CardOptions(); cardOptions.Factions = repository.Context.Factions.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); cardOptions.Series = repository.Context.Series.AsNoTracking().ToList().Select(x => x.FromDal(false)).ToList(); cardOptions.RuleSets = repository.Context.RuleSets.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); cardOptions.Statuses = repository.Context.Statuses.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); cardOptions.CardTypes = repository.Context.CardTypes.Include(x => x.TemplateInfo).AsNoTracking().ToList().Select(x => x.FromDal(true)).ToList(); return(Request.CreateResponse(cardOptions)); } }
public async Task <HttpResponseMessage> Get(Guid id) { var token = TokenExtracton.GetTokenFromCookie(HttpContext.Current.Request); using (var repository = new Repository(token)) { var card = await repository.Context.Cards.FindByGuidAsync(id); await repository.Context.Entry(card).Reference(x => x.Status).LoadAsync(); await repository.Context.Entry(card).Reference(x => x.Creator).LoadAsync(); var cardOptions = new CardOptions(); if (repository.ServiceUser != null) { await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); if (card.Status.Guid != PredefinedGuids.Final) { if (card.Creator.Guid == repository.ServiceUser.Guid) { cardOptions.IsEditable = true; } } if (repository.ServiceUser.Role.Guid == PredefinedGuids.Developer || repository.ServiceUser.Role.Guid == PredefinedGuids.Administrator || repository.ServiceUser.Role.Guid == PredefinedGuids.ServiceUser) { cardOptions.IsEditable = true; cardOptions.IsStatusChangedAllowed = true; } } cardOptions.Factions = repository.Context.Factions.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); cardOptions.Series = repository.Context.Series.AsNoTracking().ToList().Select(x => x.FromDal(false)).ToList(); cardOptions.RuleSets = repository.Context.RuleSets.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); cardOptions.Statuses = repository.Context.Statuses.AsNoTracking().ToList().Select(x => x.FromDal()).ToList(); cardOptions.CardTypes = repository.Context.CardTypes.Include(x => x.TemplateInfo).AsNoTracking().ToList().Select(x => x.FromDal(true)).ToList(); return(Request.CreateResponse(cardOptions)); } }