public async Task <ProductForDetail> GetProductDetail(int id)
        {
            var product = await _db.Products
                          .Include(p => p.Photos)
                          .Include(p => p.Category)
                          .FirstOrDefaultAsync(u => u.Id == id);

            var photosToReturn = new List <PhotoForDetail>();

            if (product == null)
            {
                return(null);
            }
            if (product.Photos.Count > 0)
            {
                foreach (var photo in product.Photos)
                {
                    var productPhoto = new PhotoForDetail
                    {
                        Id          = photo.Id,
                        Url         = photo.Url,
                        Description = photo.Description,
                        DateAdded   = photo.DateAdded,
                        IsMain      = photo.IsMain
                    };

                    photosToReturn.Add(productPhoto);
                }

                var productForReturn = new ProductForDetail
                {
                    Id             = product.Id,
                    ProductName    = product.ProductName,
                    CategoryId     = product.CategoryId,
                    CategoryName   = product.Category.CategoryName,
                    Price          = product.Price,
                    Description    = product.Description,
                    ProductDetails = product.ProductDetails,
                    Weight         = product.Weight,
                    Company        = product.Company,
                    Photos         = photosToReturn,
                };
                return(productForReturn);
            }
            return(new ProductForDetail
            {
                Id = product.Id,
                ProductName = product.ProductName,
                CategoryId = product.CategoryId,
                CategoryName = product.Category.CategoryName,
                Price = product.Price,
                //PhotoUrl = product.Photos.FirstOrDefault(p => p.IsMain).Url,
                Description = product.Description,
                ProductDetails = product.ProductDetails,
                Photos = null
            });
        }
示例#2
0
        public async Task <UserForDetail> GetUser(int id)
        {
            var user = await _context.Users
                       .Include(p => p.Photos)
                       .FirstOrDefaultAsync(u => u.Id == id);                   // 1

            if (user == null)
            {
                return(null);
            }

            var photosToReturn = new List <PhotoForDetail>();                   // 2

            foreach (var photo in user.Photos)                                  // 3
            {
                var userPhoto = new PhotoForDetail
                {
                    Id          = photo.Id,
                    Url         = photo.Url,
                    Description = photo.Description,
                    DateAdded   = photo.DateAdded,
                    IsMain      = photo.IsMain
                };

                photosToReturn.Add(userPhoto);
            }

            var userToReturn = new UserForDetail                                // 4
            {
                Id           = user.Id,
                Username     = user.Username,
                Specialty    = user.Specialty,
                Age          = user.DateOfBirth.CalculateAge(),                 //  (extension method)
                KnownAs      = user.KnownAs,
                Created      = user.Created,
                LastActive   = user.LastActive,
                Introduction = user.Introduction,
                LookingFor   = user.LookingFor,
                Interests    = user.Interests,
                City         = user.City,
                State        = user.State,
                PhotoUrl     = user.Photos.FirstOrDefault(p => p.IsMain).Url,
                Photos       = photosToReturn
            };

            return(userToReturn);                                                // 5
        }
示例#3
0
        public async Task <BlogForDetails> GetBlogById(int blogId)
        {
            var blog = await _db.Blogs
                       .Include(p => p.CategoryBlog)
                       .Include(p => p.PhotoBlog)
                       .FirstOrDefaultAsync(p => p.BlogId == blogId);

            //Trường hợp blog chưa có hình ảnh đại diên
            if (blog.PhotoBlog == null)
            {
                return(new BlogForDetails
                {
                    BlogId = blog.BlogId,
                    BlogCategoryId = blog.CategoryBlog.CategoryBlogId,
                    BlogCategoryName = blog.CategoryBlog.CategoryBlogName,
                    Title = blog.Title,
                    ShortDescription = blog.ShortDescription,
                    Content = blog.Content,
                    Photo = null
                });
            }
            //Trường hợp blog có đầy đủ thông tin
            var photo = await _db.PhotoBlogs
                        .Include(p => p.Blog)
                        .FirstAsync(p => p.BlogId == blogId);

            var photoReturn = new PhotoForDetail
            {
                Id          = photo.Id,
                Url         = photo.Url,
                Description = photo.Description,
                DateAdded   = photo.DateAdded,
                IsMain      = photo.IsMain
            };

            return(new BlogForDetails
            {
                BlogId = blog.BlogId,
                BlogCategoryId = blog.CategoryBlog.CategoryBlogId,
                BlogCategoryName = blog.CategoryBlog.CategoryBlogName,
                Title = blog.Title,
                ShortDescription = blog.ShortDescription,
                Content = blog.Content,
                Photo = photoReturn
            });
        }