Exemple #1
0
        /// <summary>
        /// Método que cria um trabalho com data, jobpost, status
        /// e os seus participantes
        /// </summary>
        /// <param name="id">Id do Employer que pretende criar o Job</param>
        /// <param name="model">Modelo do Trabalho com a informação pretendida</param>
        /// <returns>O modelo de trabalho com a informação pretendida</returns>
        public Job Create(int id, Job model)
        {
            IMateDAO <Mate> MateDAO = new MateDAO(_connection);

            if (MateDAO.FindMateById(model.Mate) == null)
            {
                return(null);
            }

            IJobDAO jobDao = new JobDAO(_connection);

            if (jobDao.FindById(model.JobPost) == null)
            {
                return(null);
            }

            using (SqlCommand cmd = _connection.Fetch().CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO dbo.[Job] (Date, MateId, JobPostId, FinishedConfirmedByEmployer, FinishedConfirmedByMate, EmployerId)" +
                                  "VALUES (@Date, @MId, @postId, @fshEmpl, @fshMt, @EId); SELECT @@Identity";

                cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = model.Date;
                cmd.Parameters.Add("@MId", SqlDbType.Int).Value       = model.Mate;
                cmd.Parameters.Add("@postId", SqlDbType.Int).Value    = model.JobPost;
                cmd.Parameters.Add("@fshEmpl", SqlDbType.Bit).Value   = false;
                cmd.Parameters.Add("@fshMt", SqlDbType.Bit).Value     = false;
                cmd.Parameters.Add("@EId", SqlDbType.Int).Value       = id;

                model.Id       = int.Parse(cmd.ExecuteScalar().ToString());
                model.Employer = (int)id;
            }

            using (SqlCommand cmd = _connection.Fetch().CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO dbo.[PendingJobs] (JobId, MateId, EmployerId)" +
                                  "VALUES (@jId, @mId, @eId); SELECT @@Identity";

                cmd.Parameters.Add("@jId", SqlDbType.Int).Value = model.Id;
                cmd.Parameters.Add("@mId", SqlDbType.Int).Value = model.Mate;
                cmd.Parameters.Add("@eId", SqlDbType.Int).Value = id;

                cmd.ExecuteScalar();
            }

            using (SqlCommand cmdSetJobPostAsDone = _connection.Fetch().CreateCommand())
            {
                cmdSetJobPostAsDone.CommandText = "UPDATE dbo.[JobPosts]" +
                                                  "SET IsDone = 1 " +
                                                  "WHERE dbo.[JobPosts].Id = @Id";

                cmdSetJobPostAsDone.Parameters.Add("@Id", SqlDbType.Int).Value = model.JobPost;

                cmdSetJobPostAsDone.ExecuteScalar();
            }

            return(model);
        }
        /// <summary>
        /// Metodo para ignorar um jobPost
        /// </summary>
        /// <param name="mateId"></param>
        /// <param name="job"></param>
        /// <returns></returns>
        public bool IgnoreJobPost(int mateId, IgnoredJobModel job)
        {
            JobDAO  jobDAO  = new JobDAO(_connection);
            JobPost jobPost = jobDAO.FindById(job.Id);

            if (jobPost == null)
            {
                return(false);
            }

            using (SqlCommand cmd = _connection.Fetch().CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO dbo.[IgnoredJobs] (MateId, JobPostId) " +
                                  "VALUES (@MtId, @JbpstId); SELECT @@Identity";

                cmd.Parameters.Add("@MtId", SqlDbType.Int).Value    = mateId;
                cmd.Parameters.Add("@JbpstId", SqlDbType.Int).Value = job.Id;

                cmd.ExecuteScalar();
            }

            return(true);
        }
Exemple #3
0
        /// <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);
        }