Ejemplo n.º 1
0
        public async Task <ActionResult <Comments> > PostComments(Comments comments)
        {
            // obtengo (si hay) usuario logueado
            Users sessionUser = _repousers.GetUserByLogin(HttpContext.Session.GetString("SessionUser"), HttpContext.Session.GetString("SessionPass"));

            // Solo los lectores (ningun usuario logueado) pueden crear comentarios
            if (sessionUser == null)
            {
                // Si la fecha de creacion es null le asigno fecha actual
                if (comments.CreatedDate == null)
                {
                    comments.CreatedDate = DateTime.Now;
                }
                comments.IsActive = true;
                _context.Comments.Add(comments);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetComments", new { id = comments.CommentID }, comments));
            }
            else
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 2
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());
        }