Example #1
0
        public async Task <IActionResult> PutCommentModel(int id, [FromBody] CommentModel commentModel)
        {
            if (id != commentModel.Id)
            {
                return(BadRequest());
            }

            _context.Entry(commentModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CommentModelExists(id))
                {
                    return(NotFound());
                }

                return(BadRequest());
            }

            return(NoContent());
        }
        public async Task <Comment> CreateCommentAsync(Comment comment)
        {
            context.Comments.Add(comment);

            await context.SaveChangesAsync();

            return(comment);
        }
Example #3
0
        /// <summary>
        /// Creates the user asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>A <see cref="Task{User}"/>.</returns>
        public async Task <User> CreateUserAsync(User user)
        {
            context.Users.Add(user);

            await context.SaveChangesAsync();

            return(user);
        }
        public async Task <Article> CreateArticlesAsync(Article article)
        {
            context.Articles.Add(article);

            await context.SaveChangesAsync();

            return(article);
        }
        public async Task <ArticleCategory> CreateArticleCategoryAsync(ArticleCategory articleCategory)
        {
            context.ArticleCategories.Add(articleCategory);

            await context.SaveChangesAsync();

            return(articleCategory);
        }
Example #6
0
        public async Task <ArticleModel> Create(CreateArticleModel model)
        {
            var entityToSave = _conversor.MapTo <CreateArticleModel, Article>(model);

            _articlesDbContext.Articles.Add(entityToSave);
            await _articlesDbContext.SaveChangesAsync();

            return(_conversor.MapTo <Article, ArticleModel>(entityToSave));
        }
        public virtual async Task <TEntity> AddAsync(TEntity entity, bool autoSave = false)
        {
            await Context.AddAsync(entity);

            if (autoSave)
            {
                await Context.SaveChangesAsync();
            }
            return(entity);
        }
        public async Task <Article> PersistAsync(Article article)
        {
            var hasSimilarSlug = await HasAnyBySlugAsync(article.Slug);

            if (hasSimilarSlug)
            {
                var guidPart = Guid.NewGuid().ToString().Substring(23);
                article.Slug += guidPart;
            }

            await _dbContext.Articles.AddAsync(article);

            await _dbContext.SaveChangesAsync();

            return(article);
        }
Example #9
0
        public async Task HandleAsync(CreateArticle command)
        {
            try
            {
                var optionsBuilder = new DbContextOptionsBuilder <ArticlesDbContext>();
                optionsBuilder.UseMySql(_configuration.GetConnectionString("Default"));

                using (var context = new ArticlesDbContext(optionsBuilder.Options))
                {
                    context.Articles.Add(new Domain.Article(command.Id, command.Name, command.Content));
                    await context.SaveChangesAsync();
                }
            }
            catch (System.Exception ex)
            {
                throw;
            }
        }
Example #10
0
        public async Task <Result <UserDto> > RegisterUser(string userId)
        {
            if (await context.Users.AnyAsync(x => x.UserId == userId))
            {
                return(Result <UserDto> .Failure(new List <string> {
                    string.Format(UsersConstants.UserAlreadyExists, userId)
                }));
            }

            var user = new User
            {
                UserId = userId
            };

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            var userToReturn = user
                               .MapTo <UserDto>();

            return(Result <UserDto> .SuccessWith(userToReturn));
        }
Example #11
0
        public async Task HandleAsync(CreateArticle command)
        {
            _context.Articles.Add(new Domain.Article(command.Id, command.Name, command.Content));

            await _context.SaveChangesAsync();
        }