コード例 #1
0
        public async Task Remove(string id, CancellationToken cancellationToken)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            try
            {
                DATABASE.Comment dbComment = await _context.Comments
                                             .FirstOrDefaultAsync(x => x.Id == id);

                if (dbComment == null)
                {
                    throw new ServiceException("Invalid comment to remove");
                }

                _context.Comments.Remove(dbComment);
                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                throw new ServiceException($"Failed to remove comment.", ex);
            }
        }
コード例 #2
0
        public async Task Edit(Comment comment, CancellationToken cancellationToken)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }

            try
            {
                DATABASE.Comment dbComment = await _context.Comments
                                             .FirstOrDefaultAsync(x => x.Id == comment.Id);

                if (dbComment == null)
                {
                    throw new ServiceException("Invalid comment to edit");
                }

                dbComment.Text = comment.Text;
                dbComment.Date = DateTime.Now;

                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                throw new ServiceException($"Failed to edit comment.", ex);
            }
        }
コード例 #3
0
        private DATABASE.Comment ConvertComment(Comment value)
        {
            var comment = new DATABASE.Comment
            {
                ByUserId = value.ByUserId,
                Id       = value.Id,
                Text     = value.Text,
                Date     = value.Date,
            };

            return(comment);
        }
コード例 #4
0
        private Comment ConvertComment(DATABASE.Comment value, string userName = null)
        {
            var comment = new Comment
            {
                ByUserId   = value.ByUserId,
                Id         = value.Id,
                Text       = value.Text,
                Date       = value.Date,
                ByUserName = userName,
            };

            return(comment);
        }
コード例 #5
0
        public async Task Create(Comment comment, CancellationToken cancellationToken = default)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }

            try
            {
                DATABASE.Comment dbComment = ConvertComment(comment);

                await _context.Comments.AddAsync(dbComment, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                throw new ServiceException($"Failed to create comment.", ex);
            }
        }
コード例 #6
0
        public async Task <Comment> Get(string id, CancellationToken cancellationToken)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            try
            {
                DATABASE.Comment dbComment = await _context.Comments
                                             .FirstOrDefaultAsync(x => x.Id == id);

                if (dbComment == null)
                {
                    throw new ServiceException("Invalid comment to get");
                }

                return(ConvertComment(dbComment));
            }
            catch (Exception ex)
            {
                throw new ServiceException($"Failed to get comment.", ex);
            }
        }