Exemplo n.º 1
0
        public async Task <CommentModel> CreateCommentAsync(Comment comment)
        {
            _context.Comments.Add(comment);
            await _context.SaveChangesAsync();

            CommentModel commentModel = await(from c in _context.Comments
                                              join user in _context.Users on c.UserId equals user.Id
                                              where c.Id == comment.Id
                                              select new CommentModel
            {
                Id       = c.Id,
                Content  = c.Content,
                Username = user.Username
            }).FirstOrDefaultAsync();

            return(commentModel);
        }
Exemplo n.º 2
0
        public async Task <PostModel> CreatePostAsync(Post post)
        {
            _context.Posts.Add(post);
            await _context.SaveChangesAsync();

            PostModel postModel = await(from p in _context.Posts
                                        join user in _context.Users on p.UserId equals user.Id
                                        where p.Id == post.Id
                                        select new PostModel
            {
                Id       = p.Id,
                Content  = p.Content,
                Username = user.Username,
                Likes    = p.Likes,
                UserId   = user.Id
            }).FirstOrDefaultAsync();

            return(postModel);
        }
Exemplo n.º 3
0
        public async Task <User> CreateUserAsync(User user, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (await _context.Users.AnyAsync(x => x.Username == user.Username))
            {
                throw new AppException($"Username {user.Username} is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(user);
        }
Exemplo n.º 4
0
        public async Task <WeatherInfoModel> CreateWeatherInfo([Service] SqlLiteDataContext dataContext, string city, float minTemp, float maxTemp, DateTime dateTime)
        {
            Console.WriteLine(minTemp);
            WeatherInfoModel weatherInfo = new WeatherInfoModel
            {
                date    = dateTime,
                City    = city,
                MinTemp = minTemp,
                MaxTemp = maxTemp
            };
            await dataContext.WeatherInfos.AddAsync(weatherInfo);

            await dataContext.SaveChangesAsync();

            return(weatherInfo);
        }