Exemplo n.º 1
0
        public static void load()
        {
            Stream stream = File.OpenWrite("temp.txt");

            try
            {
                tasks = new List <TaskStruct>();
                if (!File.Exists(TaskFilePath))
                {
                    Stream s = File.Create(TaskFilePath);
                    s.Close();
                }

                stream = File.OpenRead(TaskFilePath);
                BinaryFormatter formatter = new BinaryFormatter();
                while (stream.Position < stream.Length)
                {
                    TaskStruct currentTask = (TaskStruct)formatter.Deserialize(stream);
                    tasks.Add(currentTask);
                }
            }
            catch (Exception e)
            {
                Logger.Log.Fatal("faild to load file cue to excpetion " + e);
                Console.WriteLine("faild to load file due to exception: " + e.Message);
            }
            stream.Close();
        }
Exemplo n.º 2
0
        public static TaskStruct GetTask(int Task_id)
        {
            SQLiteCommand    command = new SQLiteCommand();
            SQLiteDataReader reader  = null;

            try
            {
                string     ans  = "";
                TaskStruct task = new TaskStruct();
                DAL.OpenConnect();
                String commandText = "SELECT * FROM Tasks WHERE Tid = @Task_id";

                command = new SQLiteCommand(commandText, DAL.connection);
                SQLiteParameter taskID = new SQLiteParameter("@Task_id", Task_id);
                command.Parameters.Add(taskID);
                command.Prepare();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    int tid = int.Parse("" + reader["Tid"]);
                    task = new TaskStruct(tid, "" + reader["Title"], "" + reader["Description"], DateTime.Parse((String)reader["DueDate"]), DateTime.Parse((String)reader["CreationDate"]));
                    ans += "id: " + tid + ";    title: " + reader["Title"] + ";    "
                           + "desciption: " + reader["Description"] + ";    DueDate: " + DateTime.Parse((String)reader["DueDate"]) +
                           ";     CreationDate: " + DateTime.Parse((String)reader["CreationDate"]) + ";    Column id: " + reader["Cid"];
                    ans += "\n";
                }
                if (task.Title == null)
                {
                    Logger.Log.Error("failed to get task id:" + Task_id + " since it doesn't exist");
                }
                else
                {
                    Logger.Log.Info("got Task with id: " + Task_id);
                }
                command.Dispose();
                reader.Close();

                DAL.CloseConnect();
                return(task);
            }
            catch (SQLiteException e)
            {
                Logger.Log.Fatal("sql exception from getting task with id: " + Task_id + "/n" + e.Message);
                if (reader != null)
                {
                    reader.Close();
                }
                command.Dispose();
                DAL.CloseConnect();
            }
            return(new TaskStruct());
        }
Exemplo n.º 3
0
        public static void UpdateTask(TaskStruct task, int Column_id)
        {
            SQLiteCommand command = new SQLiteCommand();

            try
            {
                DAL.OpenConnect();
                command             = new SQLiteCommand(null, DAL.connection);
                command.CommandText = "UPDATE Tasks " +
                                      " SET Title =  @Task_Title , Description = @Task_Des, DueDate = @Task_DD, CreationDate = @Task_CD, Cid = @Column_id    " +
                                      " WHERE (Tid = @Task_id) ";
                //  command.CommandText = "UPDATE Tasks  SET Title =  @Task_Title , Description = @Task_Des, DueDate = @Task_DD, CreationDate = @Task_CD, Cid = @Column_id  WHERE Tid = @Task_id ";
                SQLiteParameter taskID    = new SQLiteParameter("@Task_id", task.ID);
                SQLiteParameter taskTitle = new SQLiteParameter("@Task_Title", task.Title);
                SQLiteParameter taskDes   = new SQLiteParameter("@Task_Des", task.Description);
                SQLiteParameter taskDD    = new SQLiteParameter("@Task_DD", task.DueDate.ToString());
                SQLiteParameter taskCD    = new SQLiteParameter("@Task_CD", task.CreationDate.ToString());
                SQLiteParameter ColumnID  = new SQLiteParameter("@Column_id", Column_id);
                command.Parameters.Add(taskID);
                command.Parameters.Add(taskTitle);
                command.Parameters.Add(taskDes);
                command.Parameters.Add(taskDD);
                command.Parameters.Add(taskCD);
                command.Parameters.Add(ColumnID);
                command.Prepare();
                int changes = command.ExecuteNonQuery();
                command.Dispose();

                DAL.CloseConnect();
                if (changes == 0)
                {
                    Logger.Log.Error("faild to update task with id: " + task.ID + " since it does not exist");
                }
                else
                {
                    Logger.Log.Info("updated task with id: " + task.ID + "; title: '" + task.Title + "'; desciption: '" + task.Description + "'; dueDate: " + task.DueDate.Date + "; creationDate: " + task.CreationDate.Date);
                }
            }
            catch (SQLiteException e)
            {
                Logger.Log.Fatal("sql exception from updating task with id: " + task.ID + "/n" + e.Message);
                command.Dispose();
                DAL.CloseConnect();
            }
        }
Exemplo n.º 4
0
        public static void update(TaskStruct task)
        {
            TaskStruct taskToUpdate = new TaskStruct();

            foreach (var item in tasks)
            {
                if (item.ID == task.ID)
                {
                    taskToUpdate = item;
                }
            }
            if (taskToUpdate.Title == null) //impossible for real tasks
            {
                Logger.Log.Error("faile to update task id: " + task.ID + "since id doesnot exist");
                Console.WriteLine("faild to update task since it doesn;t exist");
            }
            else
            {
                Stream stream = File.Create(TaskFilePath);
                try
                {
                    tasks.Remove(taskToUpdate);
                    tasks.Add(task);

                    BinaryFormatter formatter = new BinaryFormatter();
                    foreach (var item in tasks)
                    {
                        formatter.Serialize(stream, item);
                    }
                }
                catch (Exception e)
                {
                    Logger.Log.Fatal("faild to update task id: " + task.ID + " because of excpetion " + e);
                    Console.WriteLine("failed to update task because of an exceptiomn: " + e.Message);
                }
                stream.Close();
            }
        }
Exemplo n.º 5
0
        public static void saveTask(TaskStruct task)
        {
            Stream stream = File.OpenWrite(TaskFilePath);

            try
            {
                tasks.Add(task);
                stream.Position = stream.Length;
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, task);
                Logger.Log.Info("saved new task; id: " + task.ID + "; title: '" + task.Title + "'; desciption: '" + task.Description + "'; dueDate: " + task.DueDate.Date + "; creationDate: " + task.CreationDate.Date);
            }
            catch (Exception e)
            {
                Logger.Log.Fatal("failed to save new task; id: " + task.ID + "; title: '" + task.Title + "'; desciption: '" + task.Description + "'; dueDate: " + task.DueDate.Date + "; creationDate: " + task.CreationDate.Date + "\n exception " + e);
                if (tasks.Contains(task))
                {
                    delete(task.ID);
                }
                Console.WriteLine("failed to save task because of an excpetion: " + e.Message);
            }
            stream.Close();
        }
Exemplo n.º 6
0
        public static void SaveTask(TaskStruct task, int Column_id)
        {
            SQLiteCommand command = new SQLiteCommand();

            try
            {
                DAL.OpenConnect();

                command             = new SQLiteCommand(null, DAL.connection);
                command.CommandText = "INSERT INTO Tasks(Tid, Title, Description, DueDate, CreationDate, Cid)  " +
                                      " VALUES (@Task_id, @Task_Title, @Task_Des,  @Task_DD, @Task_CD, @Column_id )";
                SQLiteParameter taskID    = new SQLiteParameter("@Task_id", task.ID);
                SQLiteParameter taskTitle = new SQLiteParameter("@Task_Title", task.Title);
                SQLiteParameter taskDes   = new SQLiteParameter("@Task_Des", task.Description);
                SQLiteParameter taskDD    = new SQLiteParameter("@Task_DD", task.DueDate.ToString());
                SQLiteParameter taskCD    = new SQLiteParameter("@Task_CD", task.CreationDate.ToString());
                SQLiteParameter ColumnID  = new SQLiteParameter("@Column_id", Column_id);
                command.Parameters.Add(taskID);
                command.Parameters.Add(taskTitle);
                command.Parameters.Add(taskDes);
                command.Parameters.Add(taskDD);
                command.Parameters.Add(taskCD);
                command.Parameters.Add(ColumnID);
                command.Prepare();
                int changes = command.ExecuteNonQuery();
                command.Dispose();

                DAL.CloseConnect();
                Logger.Log.Info("saved new task; id: " + task.ID + "; title: '" + task.Title + "'; desciption: '" + task.Description + "'; dueDate: " + task.DueDate.Date + "; creationDate: " + task.CreationDate.Date);
            }
            catch (SQLiteException e)
            {
                Logger.Log.Fatal("sql exception from saving new task with: id: " + task.ID + "; title: '" + task.Title + "'; desciption: '" + task.Description + "'; dueDate: " + task.DueDate.Date + "; creationDate: " + task.CreationDate.Date + "; column id: " + Column_id + "\n " + e);
                command.Dispose();
                DAL.CloseConnect();
            }
        }