コード例 #1
0
        public async Task <RegisterDto> RegisterAsync(Register register)
        {
            var user = await _context.Users
                       .SingleOrDefaultAsync(u => u.Username == register.Username);

            if (user != null)
            {
                return new RegisterDto {
                           User = null, Success = false, Message = "An account with the specified username exist."
                }
            }
            ;

            var hashedPassword = BCrypt.Net.BCrypt.HashPassword(register.Password);

            user = new User
            {
                Username = register.Username,
                Password = hashedPassword,
                Role     = User.RoleUser
            };

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(new RegisterDto
            {
                User = user,
                Success = true,
                Message = "Account registration successful."
            });
        }
        public async Task <Comment> CreateCommentAsync(Guid postId, Comment comment)
        {
            await _context.Comments.AddAsync(comment);

            await _context.SaveChangesAsync();

            return(comment);
        }
コード例 #3
0
        public async Task <Post> CreatePostAsync(Post post, List <SelectTag> tags)
        {
            await _context.Posts.AddAsync(post);

            post.PostTags = new List <PostTag>();

            foreach (var tag in tags)
            {
                post.PostTags.Add(new PostTag
                {
                    PostId = post.Id,
                    TagId  = Guid.Parse(tag.Value)
                });
            }

            await _context.SaveChangesAsync();

            return(post);
        }
コード例 #4
0
        public async Task <CreateFavoritePostVm> CreateFavoritePost(FavoritePost favoritePost)
        {
            var createFavoritePostVm = new CreateFavoritePostVm();

            var exist = await _context.FavoritePosts
                        .SingleOrDefaultAsync(f => f.PostId == favoritePost.PostId && f.UserId == favoritePost.UserId);

            if (exist == null)
            {
                await _context.FavoritePosts.AddAsync(favoritePost);

                createFavoritePostVm.type = "AddFavorite";
            }
            else
            {
                _context.FavoritePosts.Remove(exist);
                createFavoritePostVm.type = "RemoveFavorite";
            }
            await _context.SaveChangesAsync();

            return(createFavoritePostVm);
        }