Example #1
0
        public bool Create(CreateTeamDTO teamDTO)
        {
            var team = _context.Teams.FirstOrDefault(t => t.Name.Equals(teamDTO.Name));

            if (team != null)
            {
                return(false);
            }
            var whiteboard = new Whiteboard
            {
                Posts = new List <Post>()
            };

            team = new Team
            {
                Name       = teamDTO.Name,
                Members    = new List <User>(),
                Whiteboard = whiteboard
            };
            _context.Teams.Add(team);
            if (_context.SaveChanges() == 0)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        public bool RegisterUser(RegisterUserDTO userDTO)
        {
            if (_context.Users.Any(u => u.Email.Equals(userDTO.Email)))
            {
                throw new MyAPIException(HttpStatusCode.BadRequest, "The user already exists");
            }
            if (!userDTO.Password.Equals(userDTO.ConfirmPassword))
            {
                throw new MyAPIException(HttpStatusCode.BadRequest, "The passwors does not match");
            }
            var team = _context.Teams.FirstOrDefault(t => t.Id == userDTO.TeamId);

            if (team == null)
            {
                throw new MyAPIException(HttpStatusCode.BadRequest, $"The team with id, {userDTO.TeamId} does not exist");
            }

            var salt           = GenerateSalt();
            var hashedPassword = Hash(userDTO.Password + salt);
            var user           = new User
            {
                FirstName = userDTO.FirstName,
                LastName  = userDTO.LastName,
                Email     = userDTO.Email,
                Password  = hashedPassword,
                Salt      = salt,
                MemberOf  = team
            };

            _context.Users.Add(user);
            if (_context.SaveChanges() == 0)
            {
                throw new MyAPIException(HttpStatusCode.InternalServerError, "Something went wrong saving the user, please try again in a moment");
            }
            return(true);
        }
        public Post CreatePost(IEnumerable <Claim> claims, CreatePostDTO postDTO)
        {
            var user       = _accountService.GetUserByClaims(claims);
            var team       = _teamService.Get(user.MemberOf.Id);
            var whiteboard = team.Whiteboard;
            var post       = new Post
            {
                PostedBy = user,
                Content  = postDTO.Content,
                Created  = DateTime.Now,
                PostedOn = whiteboard
            };

            _context.Posts.Add(post);
            _context.SaveChanges();
            return(post);
        }