Esempio n. 1
0
        public async Task <ContentForMallDto> GetContentByID(int id, bool didViewContent)
        {
            if (ContentExists(id))
            {
                ContentForMall c = await _ctx.ContentsForMall.Where(co => co.ContentId == id).Include(co => co.Category).Include(co => co.Author).SingleOrDefaultAsync();

                ContentForMallDto contentForMall = null;
                if (didViewContent)
                {
                    c.NumberOfViews++;
                    try
                    {
                        _ctx.Update(c);
                        await _ctx.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogInformation($"Cannot update number of views: {ex.Message}");
                        contentForMall = new ContentForMallDto
                        {
                            CategoryId          = c.CategoryForInformationId,
                            ContentId           = c.ContentId,
                            ContentMallPosition = c.Position,
                            OldImage            = c.ImagePath,
                            LongDescription     = c.LongDescription,
                            ShortDescription    = c.ShortDescription,
                            ShowOnHome          = c.ShowOnHome,
                            Title = c.Title,
                            CategoryForInformation = c.Category,
                            DatePosted             = c.DatePosted,
                            NoOfViews = c.NumberOfViews,
                            Author    = c.Author
                        };
                        return(contentForMall);
                    }
                }
                contentForMall = new ContentForMallDto
                {
                    CategoryId          = c.CategoryForInformationId,
                    ContentId           = c.ContentId,
                    ContentMallPosition = c.Position,
                    OldImage            = c.ImagePath,
                    LongDescription     = c.LongDescription,
                    ShortDescription    = c.ShortDescription,
                    ShowOnHome          = c.ShowOnHome,
                    Title = c.Title,
                    CategoryForInformation = c.Category,
                    DatePosted             = c.DatePosted,
                    NoOfViews = c.NumberOfViews,
                    Author    = c.Author
                };
                return(contentForMall);
            }
            return(null);
        }
Esempio n. 2
0
        public async Task <bool> AddContent(ContentForMallDto newContent)
        {
            string relativePath = null;

            if (newContent.File != null)
            {
                relativePath = _img.CreateImage(newContent.File);
            }


            ContentForMall creatingNewContent = new ContentForMall
            {
                CategoryForInformationId = newContent.CategoryId,
                DatePosted       = DateTime.Now,
                Title            = newContent.Title,
                ImagePath        = relativePath,
                LongDescription  = newContent.LongDescription,
                ShortDescription = newContent.ShortDescription,
                ShowOnHome       = newContent.ShowOnHome,
                Position         = newContent.ContentMallPosition,
                AuthorId         = newContent.AuthorId
            };

            try
            {
                _ctx.ContentsForMall.Add(creatingNewContent);
                await _ctx.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"Mall content not added: {ex.Message}");
                return(false);
            }
        }