Beispiel #1
0
        public async Task <RegisteredUser> Create(string email, string password, string timezoneId)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ApplicationException("Password is required");
            }

            if (_context.RegisteredUsers.Any(u => u.Email.Equals(email.ToLowerInvariant())))
            {
                throw new ApplicationException($"The email {email} is already registered");
            }

            (byte[] hash, byte[] salt) = HashPassword(password);

            var user = new RegisteredUser
            {
                Email        = email.ToLowerInvariant(),
                PasswordHash = hash,
                PasswordSalt = salt,
                TimezoneId   = timezoneId
            };

            await _context.RegisteredUsers.AddAsync(user);

            await _context.SaveChangesAsync();

            _logger.LogInformation("User registered");

            return(user);
        }
        public async Task <IActionResult> CreateBoard(BoardCreateModel boardModel)
        {
            var board = _mapper.Map <Board>(boardModel);

            if (_context.Boards.Any(b => b.UrlName.Equals(board.UrlName)))
            {
                return(BadRequest("Friendly name is already taken"));
            }

            var regUser = await _context.RegisteredUsers.FindAsync(User.GetUserId());

            board.OwnerId = regUser.Id;

            var boardUser = new BoardUser
            {
                RegisteredUserId = board.OwnerId,
                Name             = boardModel.UserDisplayName
            };

            board.Users = new List <BoardUser>
            {
                boardUser
            };

            await _context.Boards.AddAsync(board);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(Board), new { id = board.Id }, _mapper.Map <BoardModel>(board)));
        }
        public async Task <IActionResult> UserPrices(DateTime date)
        {
            var userId = User.GetUserId();

            var user = await _context.RegisteredUsers.Include(u => u.Weeks).ThenInclude(w => w.Records).SingleAsync(u => u.Id.Equals(userId));

            var weekDate = date.ToStartOfWeek();

            var week = user.Weeks.SingleOrDefault(w => w.WeekDate.Equals(weekDate));

            if (week == null)
            {
                week = new Week
                {
                    WeekDate = weekDate
                };
                user.Weeks.Add(week);

                await _context.SaveChangesAsync();
            }

            var model = new BoardPricesModel
            {
                WeekDate = weekDate,
                Users    = new List <PricesUserModel>
                {
                    _mapper.Map <PricesUserModel>(week)
                }
            };

            return(Ok(model));
        }
        public async Task <IActionResult> GetCurrentUser()
        {
            var user = await _context.RegisteredUsers.Include(u => u.Weeks).Include(u => u.OwnedBoards)
                       .Include(u => u.BoardUsers).ThenInclude(bu => bu.Board)
                       .SingleOrDefaultAsync(u => u.Id.Equals(User.GetUserId()));

            if (user == null)
            {
                return(NotFound());
            }

            var weekDate = DateTimeExtensions.NowInLocale(user.TimezoneId).ToStartOfWeek();

            if (!user.Weeks.Any(w => w.WeekDate.Equals(weekDate)))
            {
                user.Weeks.Add(new Week
                {
                    WeekDate = weekDate
                });
                await _context.SaveChangesAsync();
            }

            return(Ok(new UserDetailsModel
            {
                Id = user.Id,
                Weeks = user.Weeks.Select(w => w.WeekDate).OrderByDescending(d => d),
                OwnedBoards = _mapper.Map <IEnumerable <BoardModel> >(user.OwnedBoards.Where(b => !b.Deleted)),
                MemberBoards =
                    _mapper.Map <IEnumerable <BoardModel> >(user.BoardUsers.Select(bu => bu.Board)
                                                            .Where(b => !b.Deleted))
            }));
        }
Beispiel #5
0
        public async Task VerifyWeeks(TurnipContext context, Board board, string timezoneId)
        {
            var currentWeek = DateTimeExtensions.NowInLocale(timezoneId).ToStartOfWeek();

            var userIds = board.Users.Select(bu => bu.RegisteredUserId);

            foreach (var userId in userIds)
            {
                var user = await context.RegisteredUsers.Include(u => u.Weeks).SingleOrDefaultAsync(u => u.Id.Equals(userId));

                if (!user.Weeks.Any(w => w.WeekDate.Equals(currentWeek)))
                {
                    user.Weeks.Add(new Week
                    {
                        WeekDate = currentWeek
                    });
                }
            }

            await context.SaveChangesAsync();
        }