public async Task DeleteContentWithID(ContentForTabDto content)
        {
            if (ContentExists(content.ContentId))
            {
                var target = await _ctx.ContentsForTab.Where(c => c.ContentId == content.ContentId).SingleOrDefaultAsync();

                if (target != null)
                {
                    if (!string.IsNullOrEmpty(target.ImagePath))
                    {
                        if (content.ImagesToDelete == null)
                        {
                            content.ImagesToDelete = new List <string>();
                        }
                        content.ImagesToDelete.Add(target.ImagePath);
                    }
                    if (content.ImagesToDelete != null)
                    {
                        _img.DeleteImages(content.ImagesToDelete.ToList());
                    }
                    try
                    {
                        _ctx.ContentsForTab.Remove(target);
                        await _ctx.SaveChangesAsync();
                    }
                    catch (DbUpdateException ex)
                    {
                        _logger.LogInformation($"Error in deleting information: {ex.Message}");
                    }
                }
            }
        }
        public async Task <bool> UpdateContentWithID(ContentForTabDto content)
        {
            if (ContentExists(content.ContentId))
            {
                string path = null;
                if (content.File != null)
                {
                    path = _img.EditImage(content.File, content.OldImage);
                }
                var target = await _ctx.ContentsForTab.Where(c => c.ContentId == content.ContentId)
                             .Select(c => new ContentForTab
                {
                    CategoryForTabId = content.CategoryId,
                    ContentId        = c.ContentId,
                    ImagePath        = !string.IsNullOrEmpty(path) ? path : c.ImagePath,
                    LongDescription  = !string.IsNullOrEmpty(content.LongDescription) ? content.LongDescription : c.LongDescription,
                    ShortDescription = !string.IsNullOrEmpty(content.ShortDescription) ? content.ShortDescription : c.ShortDescription,
                    ShowOnHome       = content.ShowOnHome,
                    Title            = !string.IsNullOrEmpty(content.Title) ? content.Title : c.Title,
                    Position         = content.ContentPosition
                }).SingleOrDefaultAsync();

                if (target != null)
                {
                    try
                    {
                        _ctx.ContentsForTab.Update(target);
                        await _ctx.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogInformation($"tab content not updated: {ex.Message}");
                        return(false);
                    }
                    if (content.OtherImage != null)
                    {
                        List <ContentImageDto> contentImages = new List <ContentImageDto>();
                        for (int i = 0; i < content.OtherImage.Count; i++)
                        {
                            contentImages.Add(new ContentImageDto
                            {
                                File        = content.OtherImage.ElementAt(i),
                                Description = content.DescriptionForImage.ElementAt(i),
                                ExtraData   = content.ExtraData.ElementAt(i)
                            });
                        }
                        try
                        {
                            _conImage.AddContentImages(content.ContentId, contentImages);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogInformation($"Could not create other images: {ex.Message}");
                        }
                    }
                    return(true);
                }
            }
            return(false);
        }
        public async Task <bool> AddContent(ContentForTabDto newContent)
        {
            string relativePath = null;

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



            ContentForTab creatingNewContent = new ContentForTab
            {
                CategoryForTabId = newContent.CategoryId,
                Title            = newContent.Title,
                ImagePath        = relativePath,
                LongDescription  = newContent.LongDescription,
                ShortDescription = newContent.ShortDescription,
                ShowOnHome       = newContent.ShowOnHome,
                Position         = newContent.ContentPosition
            };

            try
            {
                _ctx.ContentsForTab.Add(creatingNewContent);
                await _ctx.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"Tab content not added: {ex.Message}");
                return(false);
            }
            if (newContent.OtherImage != null)
            {
                List <ContentImageDto> contentImages = new List <ContentImageDto>();
                for (int i = 0; i < newContent.OtherImage.Count; i++)
                {
                    contentImages.Add(new ContentImageDto
                    {
                        File        = newContent.OtherImage.ElementAt(i),
                        Description = newContent.DescriptionForImage.ElementAt(i),
                        ExtraData   = newContent.ExtraData.ElementAt(i)
                    });
                }
                try
                {
                    _conImage.AddContentImages(creatingNewContent.ContentId, contentImages);
                }
                catch (Exception ex)
                {
                    _logger.LogInformation($"Could not create other images: {ex.Message}");
                }
            }
            return(true);
        }
예제 #4
0
        public async Task <IActionResult> Create(ContentForTabDto content)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            try
            {
                await _con.AddContent(content);

                // TODO: Add insert logic here

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(RedirectToAction("Error", "Home"));
            }
        }
예제 #5
0
        public async Task <IActionResult> Edit(int id, ContentForTabDto content)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Edit", new { id }));
            }
            if (id != content.ContentId)
            {
                return(RedirectToAction("Edit", new { id }));
            }
            try
            {
                await _con.UpdateContentWithID(content);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(RedirectToAction("Error", "Home"));
            }
        }