Esempio n. 1
0
        public async Task <IActionResult> DeactivatePosts(Guid id)
        {
            // obtengo (si hay) usuario logueado
            Users sessionUser = _repousers.GetUserByLogin(HttpContext.Session.GetString("SessionUser"), HttpContext.Session.GetString("SessionPass"));

            if (sessionUser != null)
            {
                // Si hay usuario logueado me fijo el perfil que tiene
                Profiles profile = _repoprofiles.GetProfileByID(sessionUser.ProfileID);
                if (profile.Name == "Editor")
                {
                    // Obtengo el Post a desactivar
                    Posts posts = _repoposts.GetPostsByID(id);

                    // Chequeo que no este actualmente inactivo
                    if (posts.IsActive == false)
                    {
                        return(BadRequest());
                    }

                    // Desactivo y agrego datos de actualizacion
                    posts.IsActive    = false;
                    posts.UpdatedByID = sessionUser.UserID;
                    posts.UpdatedDate = DateTime.Now;

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

                    try
                    {
                        await _context.SaveChangesAsync();

                        return(Ok());
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!PostsExists(id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            return(NoContent());
        }
Esempio n. 2
0
        public async Task <ActionResult <IEnumerable <Comments> > > GetComments()
        {
            // Obtengo todos los comentarios
            List <Comments> comments = await _context.Comments.ToListAsync();

            // obtengo (si hay) usuario logueado
            Users sessionUser = _repousers.GetUserByLogin(HttpContext.Session.GetString("SessionUser"), HttpContext.Session.GetString("SessionPass"));

            Profiles profile = new Profiles();

            if (sessionUser != null)
            {
                // Si hay usuario logueado me fijo el perfil que tiene
                profile = _repoprofiles.GetProfileByID(sessionUser.ProfileID);
            }

            // Si el usuario es de tipo Editor devuelvo la lista completa
            if (profile != null && profile.Name == "Editor")
            {
                // Chequeo que la lista no esta vacia
                if (comments == null)
                {
                    return(NoContent());
                }

                return(comments);
            }

            // Si no es editor sigo con el filtro
            // Creo una lista donde se van a guardar los comentarios despues del filtro
            List <Comments> listaComments = new List <Comments>();

            // Creo una variable Post
            Posts post = new Posts();

            // recorro los comentarios, si coincide con el post que estoy buscando y esta activo lo agrego a la lista
            foreach (Comments comment in comments)
            {
                // Obtengo el post correspondiente al comentario
                post = _repoPosts.GetPostsByID(comment.PostID);

                // Chequeo que el post este activo y aprobado
                if (post.IsActive == true && post.Status == 3)
                {
                    // Chequeo que el comentario este activo
                    if (comment.IsActive == true)
                    {
                        // Agrego a la lista
                        listaComments.Add(comment);
                    }
                }
            }

            // Chequeo que la lista no esta vacia
            if (listaComments.Count == 0)
            {
                return(NoContent());
            }

            return(listaComments);
        }