public object Get(GetPictures request) { try { var count = Db.GetScalar<Picture, int>(r => Sql.Count(r.Id)); var skip = (request.PageNumber - 1)*request.PageSize; if (skip >= count) return new HttpResult(new PageResult<PictureDTO>(new PictureDTO[0], null, count), MimeTypes.Json); var pictures = Db.Select<Picture>(q => q.Limit(skip, request.PageSize)); var minId = pictures.First().Id; var maxId = pictures.Last().Id; var pba = Db.Select<PicturesByArtist>(p => p.PictureId >= minId && p.PictureId <= maxId) .ToLookup(x => x.PictureId, x => x.ArtistId); var dto = new PageResult<PictureDTO>( pictures.Select(p => { var pictureDto = Mapper.Map<PictureDTO>(p); pictureDto.Artists = pba.Where(x => x.Key == p.Id).SelectMany(x => x).Select(x => new CustomEntity {Id = x}).ToArray(); return pictureDto; }), new UriBuilder(Request.AbsoluteUri) {Query = $"page={request.PageNumber + 1}&page_size={request.PageSize}"}.Uri, count); return new HttpResult(dto, $"{MimeTypes.Json}; charset=utf-8"); } catch { return new HttpError(InternalServerError, "Internal Server Error"); } }
public object Get(ArtistRoutes.GetArtistPictures request) { try { var artist = Db.GetByIdOrDefault<Artist>(request.Id); if (artist == null) return HttpError.NotFound("Artist with this id is not found"); var pbta = Db.Select<PicturesByArtist>(x => x.ArtistId == artist.Id); var skip = (request.PageNumber - 1)*request.PageSize; if (skip >= pbta.Count) return new HttpResult(new PageResult<PictureRoutes.PictureDTO>(new PictureRoutes.PictureDTO[0], null, pbta.Count), MimeTypes.Json); var pictures = Db.Select<Picture>().Join(pbta, p => p.Id, p => p.PictureId, (p, _) => p).Skip(skip).Take(request.PageSize).ToList(); //.Limit(skip, request.PageSize)); var minId = pictures.First().Id; var maxId = pictures.Last().Id; var pba = Db.Select<PicturesByArtist>(p => p.PictureId >= minId && p.PictureId <= maxId) .Join(pictures, p => p.PictureId, p => p.Id, (p, _) => p) .ToLookup(x => x.PictureId, x => x.ArtistId); var dto = new PageResult<PictureRoutes.PictureDTO>( pictures.Select(p => { var pictureDto = Mapper.Map<PictureRoutes.PictureDTO>(p); pictureDto.Artists = pba.Where(x => x.Key == p.Id).SelectMany(x => x).Select(x => new CustomEntity {Id = x}).ToArray(); return pictureDto; }), new UriBuilder(Request.AbsoluteUri) {Query = $"page={request.PageNumber + 1}&page_size={request.PageSize}"}.Uri, pbta.Count); return new HttpResult(dto, $"{MimeTypes.Json}; charset=utf-8"); } catch { return new HttpError(HttpStatusCode.InternalServerError, "Internal Server Error"); } }