Esempio n. 1
0
        public ActionResult CreateOrEdit(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(View(nameof(Edit), post));
            }

            TblPost dbPost = _context.TblPost.Find(post.PostId);

            if (dbPost == null)
            {
                dbPost             = new TblPost();
                dbPost.CreatedDate = DateTime.Now;
                dbPost.CategoryId  = post.CategoryId;
            }

            dbPost.Title   = post.Title;
            dbPost.Content = post.Content;

            if (!TryValidateModel(dbPost))
            {
                return(View(nameof(Edit), post));
            }

            _context.TblPost.AddOrUpdate(dbPost);
            _context.SaveChanges();

            TempData["message"]           = "Post Updated/Created!";
            TempData["messageColorClass"] = "alert-success";
            return(RedirectToAction(nameof(Show), new { id = dbPost.PostId }));
        }
        protected override void Seed(Models.AppDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            for (int i = 1; i <= 10; i++)
            {
                var category = new TblCategory
                {
                    Name        = $"DAW{i}",
                    Description = "A place to discuss DAW project",
                    CategoryId  = i
                };


                context.TblCategory.AddOrUpdate(category);
                context.SaveChanges();

                var post = new TblPost
                {
                    CategoryId  = category.CategoryId,
                    Title       = $"project {i} progress update",
                    Content     = $"Started The project {i}",
                    CreatedDate = DateTime.Now,
                    PostId      = i
                };

                context.TblPost.AddOrUpdate(post);
                context.SaveChanges();
            }

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.
        }
Esempio n. 3
0
        public async Task <IActionResult> PutTblPost(int id, TblPost tblPost)
        {
            if (id != tblPost.IdPost)
            {
                return(BadRequest());
            }

            _context.Entry(tblPost).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TblPostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 4
0
        public async Task <ActionResult <TblPost> > PostTblPost(TblPost tblPost)
        {
            _context.TblPost.Add(tblPost);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetTblPost", new { id = tblPost.IdPost }, tblPost));
        }
Esempio n. 5
0
        public ActionResult Listar(TblPost Post, int?numPagina)
        {
            PostNegocio    postNegocio = new PostNegocio();
            List <TblPost> Posts       = new List <TblPost>();

            Posts = postNegocio.ListarPosts();

            //Retorna a lista páginada na tela
            return(PartialView("PartialLista", Posts));
        }
Esempio n. 6
0
        // GET: /<controller>/
        public IActionResult Index()
        {
            TblPost post = new TblPost();

            //Guarda os dados para montar os combos
            this.ListarAutores();
            this.ListarCategorias();

            return(View(post));
        }
Esempio n. 7
0
        public void Inserir(TblPost categoria)
        {
            PostDados categoriaDados = new PostDados();

            try
            {
                categoriaDados.Inserir(categoria);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 8
0
 public void Inserir(TblPost categoria)
 {
     try
     {
         using (ContextBlog con = new ContextBlog())
         {
             con.TblPost.Add(categoria);
             con.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 9
0
        public void Ataulizar(TblPost categoria)
        {
            try
            {
                using (ContextBlog con = new ContextBlog())
                {
                    con.Entry(categoria).State = EntityState.Modified;

                    con.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 10
0
        public JsonResult AddPost([FromBody] TblPost post) //TblPost post
        {
            var cookie = Request.Cookies["User"];
            var person = Convert.ToInt32(cookie);

            if (cookie == null || cookie == "0")
            {
                person = 2;
            }

            int?newId = _ctx.TblPost.Max(x => (int?)x.PostId);

            if (newId == null)
            {
                newId = 1;
            }

            else
            {
                newId++;
            }

            if (ModelState.IsValid)
            {
                var persons  = _ctx.TblPerson.FirstOrDefault(x => x.PersonId == person);
                var category = _ctx.TblCategory.FirstOrDefault(x => x.CategoryId == 10);

                var savePost = new TblPost
                {
                    Person     = persons,
                    Category   = category,
                    PostId     = newId ?? 1,
                    PersonId   = person,
                    Text       = post.Text,
                    Title      = post.Title,
                    CategoryId = 10,
                    FirstName  = persons.FirstName,
                    Date       = DateTime.Now
                };

                _ctx.TblPost.Add(savePost);
                _ctx.SaveChanges();

                return(Json(new { status = "Successful" }));
            }
            return(Json(new { status = "Fail" }));
        }
Esempio n. 11
0
        public ActionResult Buscar(TblPost post)
        {
            try
            {
                this.ListarAutores();
                this.ListarCategorias();

                PostNegocio postNegocio = new PostNegocio();

                return(PartialView("PartialForm", postNegocio.Buscar(post)));
            }
            catch (Exception ex)
            {
                ExibirMensagem(ex.Message, ETipoMensagem.Erro, 99);
                return(PartialView("_Mensagem"));
            }
        }
Esempio n. 12
0
        public JsonResult Edit(TblPost post) //Ska ta in id och kunna redigera sina egna inlägg
        {
            var postId = _ctx.TblPost.FirstOrDefault(x => x.PostId == post.PostId);

            if (post.PersonId == postId.PersonId)
            {
                postId.Text  = post.Text;
                postId.Title = post.Title;
                _ctx.SaveChanges();

                return(Json(new { status = "Successfully changed" }));
            }
            else
            {
                return(Json(new { status = "You have not created this comment" }));
            }
        }
Esempio n. 13
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblPost = await _context.TblPost.FindAsync(id);

            if (TblPost != null)
            {
                _context.TblPost.Remove(TblPost);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 14
0
        public TblPost Buscar(TblPost categoria)
        {
            try
            {
                TblPost categoriaRetorno = new TblPost();

                using (ContextBlog con = new ContextBlog()){
                    categoriaRetorno = con.TblPost.Where(p => p.Id == categoria.Id).FirstOrDefault();
                }

                return(categoriaRetorno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 15
0
        public JsonResult Delete([FromBody] TblPost post) //Ska ta in id och kunna ta bort sina egna inlägg
        {
            var cookie = Request.Cookies["User"];
            var userId = Convert.ToInt32(cookie);

            var postId = _ctx.TblPost.FirstOrDefault(x => x.PostId == post.PostId);

            if (userId == postId.PersonId)
            {
                _ctx.TblPost.Remove(postId);
                _ctx.SaveChanges();
                return(Json(new { status = "Successfully Deleted" }));
            }
            else
            {
                return(Json(new { status = "You have not created this comment" }));
            }
        }
Esempio n. 16
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblPost = await _context.TblPost
                      .Include(t => t.Category)
                      .Include(t => t.Tag)
                      .Include(t => t.User).FirstOrDefaultAsync(m => m.PostId == id);

            if (TblPost == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Esempio n. 17
0
        public void Excluir(TblPost categoria)
        {
            try
            {
                using (ContextBlog con = new ContextBlog())
                {
                    con.Entry(categoria).State = categoria.Id == 0 ?
                                                 EntityState.Added :
                                                 EntityState.Deleted;

                    con.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 18
0
        public async void AddPost(PostModel postModel)
        {
            TblPost tblPost = new TblPost
            {
                Title        = postModel.Title,
                CreatedDate  = postModel.CreatedDate,
                Description  = postModel.Description,
                IsActive     = postModel.IsActive,
                IsDeleted    = postModel.IsDeleted,
                ModifiedDate = postModel.ModifiedDate,
                Tags         = postModel.Tags
            };

            using (LaantiDuniyaContext dbContext = new LaantiDuniyaContext())
            {
                dbContext.TblPost.Add(tblPost);
                await dbContext.SaveChangesAsync();
            }
        }
Esempio n. 19
0
        public ActionResult Excluir(TblPost post)
        {
            try
            {
                PostNegocio postNegocio = new PostNegocio();

                //Exclui o Post informado
                postNegocio.Excluir(post);

                //Retorna a mesagem de sucesso
                ExibirMensagem("Post excluído com sucesso", ETipoMensagem.Sucesso, 200);

                return(PartialView("_Mensagem"));
            }
            catch (Exception ex)
            {
                //Mensagem de erro do sistema
                ExibirMensagem(ex.Message, ETipoMensagem.Erro, 99);
                return(PartialView("_Mensagem"));;
            }
        }
Esempio n. 20
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblPost = await _context.TblPost
                      .Include(t => t.Category)
                      .Include(t => t.Tag)
                      .Include(t => t.User).FirstOrDefaultAsync(m => m.PostId == id);

            if (TblPost == null)
            {
                return(NotFound());
            }
            ViewData["CategoryId"] = new SelectList(_context.TblCategory, "CategoryId", "Category");
            ViewData["TagId"]      = new SelectList(_context.TblTag, "TagId", "Tag");
            ViewData["UserId"]     = new SelectList(_context.TblUser, "UserId", "UserId");
            return(Page());
        }
Esempio n. 21
0
        public ActionResult Show(int id)
        {
            TblPost post = _context.TblPost
                           .FirstOrDefault(p => p.PostId == id);

            List <Comment> comments = post.TblComments.Select(c => new Comment {
                CommentId = c.CommentId,
                PostId    = c.PostId,
                Content   = c.Content
            }).ToList();

            var model = new Post
            {
                Title      = post.Title,
                PostId     = post.PostId,
                Content    = post.Content,
                CategoryId = post.CategoryId,
                Comments   = comments
            };

            return(View(model));
        }
Esempio n. 22
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            TblPost post = new TblPost();

            if (Input.BannerImagePath != null && Input.BannerImagePath.Count > 0)
            {
                foreach (var item in Input.BannerImagePath)
                {
                    using (var stream = new MemoryStream())
                    {
                        await item.CopyToAsync(stream);

                        post.BannerImagePath = stream.ToArray();
                    }
                }
            }

            post.UserId        = Input.UserId;
            post.Title         = Input.Title;
            post.CategoryId    = Input.CategoryId;
            post.Summary       = Input.Summary;
            post.FullBody      = Input.FullBody;
            post.NumViews      = 0;
            post.DatePublished = DateTime.Today;


            _context.TblPost.Add(post);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Esempio n. 23
0
        public void Atualizar(TblPost categoria)
        {
            PostDados categoriaDados = new PostDados();

            categoriaDados.Ataulizar(categoria);
        }
Esempio n. 24
0
        public TblPost Buscar(TblPost categoria)
        {
            PostDados categoriaDados = new PostDados();

            return(categoriaDados.Buscar(categoria));
        }
Esempio n. 25
0
        public void Excluir(TblPost categoria)
        {
            PostDados categoriaDados = new PostDados();

            categoriaDados.Excluir(categoria);
        }
Esempio n. 26
0
        public ActionResult Salvar(TblPost post, string ConteudoHTML)
        {
            try
            {
                PostNegocio postNegocio = new PostNegocio();

                #region Validação dos campos (Servidor)

                // Validação do Nome
                //if (post.Nome == null)
                //{
                //    ExibirMensagem("O Nome é obrigatório", ETipoMensagem.Alerta, 99);
                //    return PartialView("_Mensagem");
                //}

                //// Validação da Sobrenome
                //if (post.SobreNome == null)
                //{
                //    ExibirMensagem("O Sobrenome é obrigatório", ETipoMensagem.Alerta, 99);
                //    return PartialView("_Mensagem");
                //}

                //// Validação do Email
                //if (post.Email == null)
                //{
                //    ExibirMensagem("O Email é obrigatório", ETipoMensagem.Alerta, 99);
                //    return PartialView("_Mensagem");
                //}

                //// Validação dos emails informados
                //if (post.Email != ConfirmarEmail)
                //{
                //    ExibirMensagem("Os emails informados não são iguais", ETipoMensagem.Alerta, 99);
                //    return PartialView("_Mensagem");
                //}

                //// Validação do Resumo
                //if (post.Resumo == null)
                //{
                //    ExibirMensagem("O Resumo é obrigatório", ETipoMensagem.Alerta, 99);
                //    return PartialView("_Mensagem");
                //}

                #endregion

                //Data de Cadastro
                post.DataCadastro = DateTime.Now;

                string teste = string.Empty;

                //HttpUtility.HtmlDecode(post.Conteudo, teste);

                post.Conteudo = WebUtility.HtmlDecode(ConteudoHTML);

                if (post.Id > 0)
                {
                    postNegocio.Atualizar(post);
                    ExibirMensagem("Post alterado com sucesso", ETipoMensagem.Sucesso, 200);
                }
                else
                {
                    postNegocio.Inserir(post);
                    ExibirMensagem("Post cadastrado com sucesso", ETipoMensagem.Sucesso, 200);
                }

                return(PartialView("_Mensagem"));
            }
            catch (Exception ex)
            {
                ExibirMensagem(ex.Message, ETipoMensagem.Erro, 99);
                return(PartialView("_Mensagem"));
            }
        }