public async Task <IActionResult> DeleteFile(int id)
        {
            var record = await context.Set <DBFile>().FirstOrDefaultAsync(a => a.Id == id);

            if (record == null)
            {
                return(NotFound());
            }

            var userId = GetUserId();

            if (record.OwnerId != userId)
            {
                return(BadRequest());
            }

            try
            {
                System.IO.File.Delete(record.DrivePath);

                await context.SaveChangesAsync();

                context.Set <DBFile>().Remove(record);

                return(NoContent());
            }
            catch (DbUpdateException ex)
            {
                Logger.Log($"{nameof(FilesController)} {nameof(DeleteFile)}", ex.Message, NLog.LogLevel.Error, ex);

                return(BadRequest());
            }
            catch (Exception ex)
            {
                Logger.Log($"{nameof(FilesController)} {nameof(DeleteFile)}", ex.Message, NLog.LogLevel.Error, ex);

                return(BadRequest());
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> DeletePost(int id)
        {
            var record = await context.Set <Post>().FindAsync(id);

            if (record == null)
            {
                return(NotFound());
            }

            var userId = GetUserId();

            if (record.AuthorId != userId)
            {
                return(BadRequest());
            }

            context.Set <Post>().Remove(record);
            context.Set <PostComment>().RemoveRange(context.Set <PostComment>().Where(a => a.PostId == id));

            try
            {
                await context.SaveChangesAsync();

                return(NoContent());
            }
            catch (DbUpdateException ex)
            {
                Logger.Log($"{nameof(PostsController)} {nameof(DeletePost)}", ex.Message, NLog.LogLevel.Warn, ex);

                return(BadRequest());
            }
            catch (Exception ex)
            {
                Logger.Log($"{nameof(PostsController)} {nameof(DeletePost)}", ex.Message, NLog.LogLevel.Warn, ex);

                return(BadRequest());
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> DeleteGroup(int id)
        {
            var record = await context.Set <Group>().Include(a => a.Members).FirstOrDefaultAsync(a => a.Id == id);

            if (record == null)
            {
                return(NotFound());
            }

            var userId = GetUserId();

            if (!record.Members.Any(a => a.UserId == userId && a.UserRank == UserRank.Leader))
            {
                return(BadRequest());
            }

            context.Set <Group>().Remove(record);

            try
            {
                await context.SaveChangesAsync();

                return(NoContent());
            }
            catch (DbUpdateException ex)
            {
                Logger.Log($"{nameof(GroupsController)} {nameof(DeleteGroup)}", ex.Message, NLog.LogLevel.Warn, ex);

                return(BadRequest());
            }
            catch (Exception ex)
            {
                Logger.Log($"{nameof(GroupsController)} {nameof(DeleteGroup)}", ex.Message, NLog.LogLevel.Warn, ex);

                return(BadRequest());
            }
        }