Exemple #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);
            }
        }
Exemple #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);
            }
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
            }
        }
Exemple #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);
            }
        }