public async Task <Response> Delete(int commentId) { if (!_userService.IsAuthenticated) { return(Respond().WithError("*", "Not Authorized")); } var comment = await _context.Comments .Include(c => c.Thread) .ThenInclude(t => t.Namespace) .ThenInclude(n => n.User) .SingleOrDefaultAsync(c => c.CommentId == commentId); if (comment == null) { return(Respond().WithError("*", "Comment doesn't exist")); } if (comment.Thread.Namespace.User.Id != _userService.GetUserId()) { return(Respond().WithError("*", "Not Authorized")); } ClearCacheKeysForThread(comment.Thread.ThreadId); _context.Comments.Remove(comment); var diff = await _context.SaveChangesAsync(); return(diff > 0 ? Respond().WithSuccess() : Respond().WithError("*", "Error deleting comment")); }
public async Task <DataResponse <NamespaceDto> > Create(CreateNamespaceDto createNamespaceDto) { var namespaceEntity = _mapper.Map <Namespace>(createNamespaceDto); namespaceEntity.User = await _userService.GetUser(); await _context.Namespaces.AddAsync(namespaceEntity); await _context.SaveChangesAsync(); return(new DataResponse <NamespaceDto>() .WithData(_mapper.Map <NamespaceDto>(namespaceEntity))); }
public async Task <Response> Rate(CreateThreadRatingDto createThreadRatingDto) { var thread = await _context.Threads. SingleOrDefaultAsync(t => t.ThreadId == createThreadRatingDto.ThreadId); //var existingThreadRating = await _context.ThreadRatings // .Include(tr => tr.Thread) // .Include(tr => tr.User) // .SingleOrDefaultAsync(tr => // tr.Thread.ThreadId == thread.ThreadId && // tr.User.Id == _userService.GetUserId()); if (!Enum.IsDefined(typeof(Rating), createThreadRatingDto.Type)) { return(new Response().WithError("*", "Invalid Rating Type")); } //if (existingThreadRating != null) //{ // //user already rated this thread, either change the rating type or do nothing // if (existingThreadRating.Type != createThreadRatingDto.Type) // { // existingThreadRating.Type = createThreadRatingDto.Type; // await _context.SaveChangesAsync(); // return new Response().WithSuccess(); // } // return new Response() // .WithError("*", "Rating already exists"); //} var threadRatingEntity = _mapper.Map <ThreadRating>(createThreadRatingDto); threadRatingEntity.CreatedOn = DateTime.UtcNow; //threadRatingEntity.User = await _userService.GetUser(); thread.ThreadRatings.Add(threadRatingEntity); await _context.SaveChangesAsync(); return(new Response().WithSuccess()); }