/// <summary> /// Método para confirmar o trabalho como realizado, /// removendo-o posteriormente da lista de trabalhos /// pendentes do Mate /// </summary> /// <param name="jobId">Id do trabalho a marcar como concluído</param> /// <param name="userId">Id do utilizador que marca o trabalho como /// concluído</param> /// <returns>Verdadeiro em caso de sucesso, falso caso contrário</returns> public bool MarkJobAsDone(int jobId, int userId) { WorkDAO workDAO = new WorkDAO(_connection); WorkDetailsModel job = workDAO.FindById(jobId); if (job == null) { throw new Exception("ID de trabalho Inválido!"); } EmployerDAO employerDAO = new EmployerDAO(_connection); Employer employer = employerDAO.FindEmployerById(userId); bool foundEmployer = (employer != null); if (foundEmployer) { using (SqlCommand cmd = _connection.Fetch().CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "UPDATE dbo.[Job] SET FinishedConfirmedByEmployer = @fnshdEmployer " + "WHERE Id = @id"; cmd.Parameters.Add("@id", SqlDbType.Int).Value = jobId; cmd.Parameters.Add("@fnshdEmployer", SqlDbType.Bit).Value = true; cmd.ExecuteNonQuery(); } return(true); } MateDAO mateDAO = new MateDAO(_connection); Mate mate = mateDAO.FindMateById(userId); bool foundMate = (mate != null); if (foundMate) { using (SqlCommand cmd = _connection.Fetch().CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "UPDATE dbo.[Job] SET FinishedConfirmedByMate = @fnshdMate " + "WHERE Id = @id"; cmd.Parameters.Add("@id", SqlDbType.Int).Value = jobId; cmd.Parameters.Add("@fnshdMate", SqlDbType.Bit).Value = true; cmd.ExecuteNonQuery(); } return(true); } throw new Exception("ID do utilizador Inválido!"); }
/// <summary> /// Método para encontrar um trabalho por Id /// </summary> /// <param name="id">Id do trabalho pretendido</param> /// <returns>Retorna o trabalho pretendido, se nao existir /// retorna null </returns> public WorkDetailsModel FindById(int id) { WorkDetailsModel wd = null; bool contains = false; int _mateId = 0; int _employerId = 0; int _postId = 0; int?_invoiceId = 0; using (SqlCommand cmd = _connection.Fetch().CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT Date, MateId, JobPostId, InvoiceId, FinishedConfirmedByEmployer, FinishedConfirmedByMate, EmployerId " + "FROM dbo.[Job] " + "WHERE Id=@id"; cmd.Parameters.Add("@id", SqlDbType.Int).Value = id; using (SqlDataReader reader = cmd.ExecuteReader()) { if (contains = reader.HasRows) { wd = new WorkDetailsModel(); reader.Read(); wd.Date = reader.GetDateTime(0); _mateId = reader.GetInt32(1); _postId = reader.GetInt32(2); //guardar o id do invoice para mais tarde fazer o find if (reader.IsDBNull(3)) { _invoiceId = null; } else { _invoiceId = reader.GetInt32(3); } wd.FinishedConfirmedByEmployer = reader.GetBoolean(4); wd.FinishedConfirmedByMate = reader.GetBoolean(5); _employerId = reader.GetInt32(6); } } } if (contains) { IMateDAO <Mate> mateDAO = new MateDAO(_connection); Mate mate = mateDAO.FindMateById(_mateId); wd.Mate = mate; IJobDAO postDao = new JobDAO(_connection); JobPost post = postDao.FindById(_postId); wd.JobPost = post; if (_invoiceId != null) { wd.InvoiceId = (int)_invoiceId; } IEmployerDAO <Employer> employerDAO = new EmployerDAO(_connection); Employer employer = employerDAO.FindEmployerById(_employerId); wd.Employer = employer; } else { return(null); } return(wd); }