/// <summary>
 /// Updates existing project's properties.
 /// </summary>
 public static void UpdateProjectProperties(int projectID, string projectName, string projectDesc, string githubUser, string githubRepo)
 {
     try
     {
         using (var db = new atlasEntities())
         {
             // Get project from DB
             project project = null;
             foreach (var p in db.projects)
             {
                 if (p.id == projectID)
                 {
                     project = p;
                     break;
                 }
             }
             if (project != null)
             {
                 // Update project's properties
                 project.name            = projectName;
                 project.description     = projectDesc;
                 project.github_username = githubUser;
                 project.github_reponame = githubRepo;
             }
             db.SaveChanges();
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
    // hakee taskien id-listan ja userID:n perusteella yhteistuntimäärän taskeista tietylle käyttäjälle
    protected static int GetWorkingHours(IEnumerable <int> tasks, int userID)
    {
        using (var db = new atlasEntities())
        {
            try
            {
                var query = (from dtask in db.donetasks
                             join t in tasks on dtask.task_id equals t
                             where dtask.task_id == t && dtask.user_id == userID
                             select dtask.worktime);

                if (query.Count() > 0)
                {
                    return(query.Sum());
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    /// <summary>
    /// Deletes given project from database.
    /// </summary>
    public static void DeleteProject(int projectID)
    {
        try
        {
            using (var db = new atlasEntities())
            {
                // Find project from DB
                project projectToDelete = null;
                foreach (project p in db.projects)
                {
                    if (p.id == projectID)
                    {
                        projectToDelete = p;
                        break;
                    }
                }

                // Delete project's foreign key from all users
                var users = db.users.ToList();
                foreach (var u in users)
                {
                    u.projects.Remove(projectToDelete);
                }

                // Delete project
                db.projects.Remove(projectToDelete);
                db.SaveChanges();
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
 public static IEnumerable <donetask> GetDonetasks(int taskId)
 {
     using (atlasEntities db = new atlasEntities())
     {
         var donetasks = (from c in db.donetasks
                          where c.task_id == taskId
                          select c).ToList();
         return(donetasks);
     }
 }
    public static IEnumerable <donetask> GetDonetasks(int taskId, int userId)
    {
        using (atlasEntities db = new atlasEntities())
        {
            var donetasks = from c in db.donetasks
                            where c.task_id == taskId && c.user_id == userId
                            select c;

            return(donetasks.ToList());
        }
    }
    public static IEnumerable <task> GetMajorTasks(int projectId)
    {
        using (atlasEntities db = new atlasEntities())
        {
            var rootTasks = from c in db.tasks
                            where c.task_id == null && c.project_id == projectId
                            select c;

            return(rootTasks);
        }
    }
 /// <summary>
 /// Gets all users from DB.
 /// </summary>
 public static List <user> GetAllUsers()
 {
     try
     {
         using (var db = new atlasEntities())
         {
             return(db.users.ToList());
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
    /// <summary>
    /// Adds given project to given user.
    /// </summary>
    public static void AddProjectToUser(string username, int projectID)
    {
        try
        {
            using (var db = new atlasEntities())
            {
                // Get user from DB
                user user = null;
                foreach (var u in db.users)
                {
                    if (u.username == username)
                    {
                        user = u;
                        break;
                    }
                }

                // Get project from DB
                project project = null;
                foreach (var p in db.projects)
                {
                    if (p.id == projectID)
                    {
                        project = p;
                        break;
                    }
                }

                // Add project to user (if user doesn't have it already)
                if (user != null && project != null)
                {
                    if (!user.projects.Contains(project))
                    {
                        user.projects.Add(project);
                    }
                }
                db.SaveChanges();
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    /// <summary>
    /// Checks if given user has given project.
    /// </summary>
    public static bool UserHasProject(string username, int projectID)
    {
        try
        {
            using (var db = new atlasEntities())
            {
                // Get user from DB
                user user = null;
                foreach (var u in db.users)
                {
                    if (u.username == username)
                    {
                        user = u;
                        break;
                    }
                }

                // Get project from DB
                project project = null;
                foreach (var p in db.projects)
                {
                    if (p.id == projectID)
                    {
                        project = p;
                        break;
                    }
                }

                // Check if user has the project
                if (user != null && project != null)
                {
                    if (user.projects.Contains(project))
                    {
                        return(true);
                    }
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
        return(false);
    }
    /// <summary>
    /// Removes given project from given user.
    /// </summary>
    public static void RemoveProjectFromUser(string username, int projectID)
    {
        try
        {
            using (var db = new atlasEntities())
            {
                // Get user from DB
                user user = null;
                foreach (var u in db.users)
                {
                    if (u.username == username)
                    {
                        user = u;
                        break;
                    }
                }

                // Get project from DB
                project project = null;
                foreach (var p in db.projects)
                {
                    if (p.id == projectID)
                    {
                        project = p;
                        break;
                    }
                }

                // Remove project from user
                if (user != null && project != null)
                {
                    if (user.projects.Contains(project))
                    {
                        user.projects.Remove(project);
                    }
                }
                db.SaveChanges();
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    // HUOM EI HAE ITSE ROOT-TASKIIN TALLENNETTUJA TYÖTUNTEJA. Tunteja ei tule tallentaa root-taskeihin.
    public static List <Task> GetProjectWorkingHoursForUser(int projectID, int userID)
    {
        try
        {
            using (var db = new atlasEntities())
            {
                List <Task>       TopTasks = new List <Task>();
                Task              tempTask;
                IEnumerable <int> tasks;
                int tempHours;

                // haetaan ensimmäisen polven taskit, joilla task_id == null
                var majorTasksQuery = from c in db.tasks
                                      where c.project_id == projectID && c.task_id == null
                                      select c;

                // iteroidaan löydettyjen taskien läpi
                foreach (var item in majorTasksQuery)
                {
                    // luo tilapäisolio taskin tiedoilla
                    tempTask = new Task(item.id, item.name);
                    // hae taskin kaikki lapset listaan
                    tasks = GetChildrenIds(item.id);
                    // hae työtunnit jokaisesta listan taskista tilapäisolion tietoihin
                    tempHours = GetWorkingHours(tasks, userID);
                    // varmistetaan että työtunteja on olemassa
                    if (tempHours > 0)
                    {
                        tempTask.Duration = tempHours;
                        // tallennetaan tilapäisolio pysyvään listaan
                        TopTasks.Add(tempTask);
                    }
                }
                return(TopTasks);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 public static int AddTask(int?parentId, int projectId, string taskName)
 {
     try
     {
         using (atlasEntities db = new atlasEntities())
         {
             task t = new task();
             t.task_id    = parentId;
             t.project_id = projectId;
             t.name       = taskName;
             db.tasks.Add(t);
             previouslyCreatedTask = t;
             int result = db.SaveChanges();
             return(result);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public static int AddDonetask(int taskId, int userId, int workTime, DateTime time)
 {
     try
     {
         using (atlasEntities db = new atlasEntities())
         {
             donetask dt = new donetask();
             dt.task_id  = taskId;
             dt.user_id  = userId;
             dt.worktime = workTime;
             dt.date     = time;
             db.donetasks.Add(dt);
             int result = db.SaveChanges();
             return(result);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 /// <summary>
 /// Gets given user from DB.
 /// </summary>
 public static user GetUser(string username)
 {
     try
     {
         using (var db = new atlasEntities())
         {
             foreach (var u in db.users)
             {
                 if (u.username == username)
                 {
                     return(u);
                 }
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
     return(null);
 }
    public static int RemoveTask(int id)
    {
        using (atlasEntities db = new atlasEntities())
        {
            // poistetaan taskiin kiinnitetyt donetaskit
            var donetasks = from c in db.donetasks where c.task_id == id select c;
            if (donetasks.Count() > 0)
            {
                foreach (donetask item in donetasks)
                {
                    db.donetasks.Remove(item);
                }
            }

            // poistetaan task
            var t = (from c in db.tasks where c.id == id select c).FirstOrDefault();
            db.tasks.Remove(t);
            // tallenna muutokset
            int result = db.SaveChanges();
            return(result);
        }
    }
 /// <summary>
 /// Gets project from DB with the given project ID.
 /// </summary>
 public static project GetProjectFromDb(int id)
 {
     try
     {
         using (var db = new atlasEntities())
         {
             var ret = from p in db.projects
                       where p.id == id
                       select p;
             var project = ret.FirstOrDefault();
             if (project != null)
             {
                 return(project);
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
     return(null);
 }
 /// <summary>
 /// Gets all projects for given user from DB.
 /// </summary>
 public static List <project> GetAllProjectsForUser(string username)
 {
     try
     {
         using (var db = new atlasEntities())
         {
             var ret = from u in db.users
                       where u.username == username
                       select u;
             var user = ret.FirstOrDefault();
             if (user != null)
             {
                 return(user.projects.ToList());
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
     return(null);
 }
 /// <summary>
 /// Adds given project to database. Returns false if project with the same name already exists.
 /// </summary>
 public static bool AddProject(project projectToAdd)
 {
     try
     {
         using (var db = new atlasEntities())
         {
             // Check if project with the same name already exists
             foreach (project p in db.projects)
             {
                 if (p.name == projectToAdd.name)
                 {
                     return(false);
                 }
             }
             db.projects.Add(projectToAdd);
             db.SaveChanges();
             return(true);
         }
     }
     catch (Exception)
     {
         throw;
     }
 }