Esempio n. 1
0
        public MotionPicture GetMotionPictureByID(int id)
        {
            MotionPicture motionPicture = new MotionPicture();

            try
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand("select * from MotionPictures where ID = @id", conn);
                    command.Parameters.AddWithValue("@id", id);
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        motionPicture = GetMotionPictureFromReader(reader);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(motionPicture);
        }
Esempio n. 2
0
        private MotionPicture GetMotionPictureFromReader(SqlDataReader reader)
        {
            MotionPicture motionPicture = new MotionPicture()
            {
                ID          = Convert.ToInt32(reader["ID"]),
                Name        = Convert.ToString(reader["Name"]),
                Description = Convert.ToString(reader["Description"]),
                ReleaseYear = Convert.ToInt32(reader["Release_Year"])
            };

            return(motionPicture);
        }
Esempio n. 3
0
        public ActionResult <MotionPicture> PostPicture(MotionPicture picture)
        {
            try
            {
                motionPictureDAO.PostMotionPicture(picture);
            }
            catch (Exception e)
            {
                StatusCode(500, e.Message);
            }

            return(Created($"/api/MotionPicture/{picture.ID}", picture));
        }
Esempio n. 4
0
        public ActionResult <MotionPicture> UpdatePicture(int id, MotionPicture updatedPicture)
        {
            if (!ModelState.IsValid || updatedPicture.ID != id)
            {
                return(BadRequest());
            }
            else
            {
                try
                {
                    motionPictureDAO.EditMotionPicture(updatedPicture);
                }
                catch (Exception e)
                {
                    return(StatusCode(500, e.Message));
                }

                return(Ok(updatedPicture));
            }
        }
Esempio n. 5
0
        public int PostMotionPicture(MotionPicture newPicture)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand("insert into MotionPictures (Name, Description, Release_Year) values (@name, @description, @release_year); select scope_identity();", conn);
                    command.Parameters.AddWithValue("@name", newPicture.Name);
                    command.Parameters.AddWithValue("@description", newPicture.Description);
                    command.Parameters.AddWithValue("@release_year", newPicture.ReleaseYear);
                    newPicture.ID = Convert.ToInt32(command.ExecuteScalar());
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(newPicture.ID);
        }
Esempio n. 6
0
        public ActionResult <MotionPicture> GetPictureByID(int id)
        {
            MotionPicture picture = new MotionPicture();

            try
            {
                picture = motionPictureDAO.GetMotionPictureByID(id);
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }

            if (picture.Name != null)
            {
                return(Ok(picture));
            }
            else
            {
                return(BadRequest());
            }
        }
Esempio n. 7
0
        public bool EditMotionPicture(MotionPicture editedMotionPicture)
        {
            int rowsAffected = 0;

            try
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand("update MotionPictures set Name = @name, Description = @description, Release_Year = @releaseYear where ID = @id;", conn);
                    command.Parameters.AddWithValue("@name", editedMotionPicture.Name);
                    command.Parameters.AddWithValue("@description", editedMotionPicture.Description);
                    command.Parameters.AddWithValue("@releaseYear", editedMotionPicture.ReleaseYear);
                    command.Parameters.AddWithValue("@id", editedMotionPicture.ID);

                    rowsAffected = command.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(rowsAffected > 0);
        }