Beispiel #1
0
        //
        // Summary:
        //     Adds a task and associates it to a given user.
        //
        // Parameters:
        //   newTask:
        //     The task to add.
        //
        //   userId:
        //     The UserId to associates the task to.
        //
        // Returns:
        //     The given task.
        //
        public Models.Task AddUserTask(Models.Task newTask, string userId)
        {
            try
            {
                long longUserId;
                if (!(long.TryParse(userId, out longUserId)))
                {
                    throw new Exception("Token not valid");
                }

                _context.Tasks.Add(newTask);

                _context.SaveChanges();

                UserService userService = new UserService(configuration, _context);

                User user = userService.GetUserById(longUserId);

                UserTaskService userTaskService = new UserTaskService(configuration, _context);

                userTaskService.AssociateTaskToUser(newTask, user);

                return(newTask);
            }
            catch (Exception err)
            {
                throw err;
            }
        }
Beispiel #2
0
        //
        // Summary:
        //     Registers a new user with all it's data and returns a valid new token.
        //
        // Parameters:
        //   User:
        //     A new user with all it's data.
        //
        // Returns:
        //     A string representing a valid new token.
        //
        public string RegisterUserAndGetToken(User user)
        {
            try
            {
                User existingUser = _context.Users
                                    .Where(usr => usr.UserName == user.UserName || usr.Email == user.Email || usr.PhoneNumber == user.PhoneNumber)
                                    .FirstOrDefault();

                if (existingUser != null)
                {
                    throw new Exception("UserName, email or phone number already taken");
                }

                AuthService auth = new AuthService(configuration);

                user.Role = "standartUser";

                user.Password = auth.HashPassword(user.Password);

                _context.Users.Add(user);

                _context.SaveChanges();

                if (user.Tasks != null && user.Tasks.Count > 0)
                {
                    TaskService taskService = new TaskService(configuration, _context);

                    taskService.AddTasks(user.Tasks);

                    UserTaskService userTaskService = new UserTaskService(configuration, _context);

                    userTaskService.AssociateTasksToUser(user.Tasks, user);
                }

                string token = auth.CreateToken(user);

                return(token);
            }
            catch (Exception err)
            {
                throw err;
            }
        }
Beispiel #3
0
        //
        // Summary:
        //     Finds a list of tasks (their Ids are given) and if they are not already associated,
        //     associate them to a new user and then sends them to the new user.
        //
        // Parameters:
        //   shareData:
        //     A shareData object containing the list of tasks to share and the new userId to share with.
        //
        //   userId:
        //     A Id of the already associated user.
        //
        // Returns:
        //     True if everything was associated and sent fine.
        //
        public bool ShareTasksWithUser(TasksShareData shareData, string userId)
        {
            try
            {
                if (shareData.TasksIdsToShare.Count == 0)
                {
                    throw new Exception("No tasks to share");
                }

                long longUserId;
                if (!(long.TryParse(userId, out longUserId)))
                {
                    throw new Exception("Token not valid");
                }

                UserService userService = new UserService(configuration, _context);

                User sharingUser = userService.GetUserById(longUserId);
                List <Models.Task> sharingUserTasks = GetUserTasks(userId);

                User        userToShare = userService.GetUserById(shareData.UserIdToShare);
                List <long> userToShareExistingTasksIds = GetUserTasks(shareData.UserIdToShare.ToString()).Select(tsk => tsk.TaskId).ToList();;

                List <long> newTasksIdsToShare = new List <long>();

                foreach (long TasksIdToShare in shareData.TasksIdsToShare)
                {
                    if (!userToShareExistingTasksIds.Contains(TasksIdToShare))
                    {
                        newTasksIdsToShare.Add(TasksIdToShare);
                    }
                }

                List <Models.Task> tasksToShare = new List <Models.Task>();

                foreach (Models.Task sharingUserTask in sharingUserTasks)
                {
                    if (newTasksIdsToShare.Contains(sharingUserTask.TaskId))
                    {
                        tasksToShare.Add(sharingUserTask);
                    }
                }

                if (tasksToShare.Count == 0)
                {
                    throw new Exception("No tasks to share");
                }

                UserTaskService userTaskService = new UserTaskService(configuration, _context);

                userTaskService.AssociateTasksToUser(tasksToShare, userToShare);

                MailService mailService = new MailService(configuration);

                string messageBodyPrefix = configuration.email.messageBodyPreprefix + " " + userToShare.FirstName + ",\n" + sharingUser.UserName + configuration.email.messageSharedTasksBodyPrefix;
                string messageBodySufix  = configuration.email.messageSuffix + configuration.email.fromName;
                string messageBody       = mailService.BuildMessageBodyFromTasks(tasksToShare, messageBodyPrefix, messageBodySufix);

                mailService.SendEmail(userToShare.UserName, userToShare.Email, sharingUser.UserName + " " + configuration.email.subjectSharedTasks, messageBody);

                return(true);
            }
            catch (Exception err)
            {
                throw err;
            }
        }