public async Task <object> ThemPhim(PhimInsert phim)
        {
            using (var connection = new SqlConnection(connectionString))
            {
                string tenPhim = LoaiBoKyTu.bestLower(phim.TenPhim);
                if (string.IsNullOrEmpty(tenPhim))
                {
                    return(await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Tên phim không hợp lệ!"));
                }
                var p = connection.Query("SELECT TenPhim FROM dbo.PHIM WHERE BiDanh = '" + tenPhim + "'", commandType: CommandType.Text);

                if (p.Count() > 1)
                {
                    return(await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Tên phim đã tồn tại!"));
                }
                phim.BiDanh = LoaiBoKyTu.bestLower(phim.TenPhim);
                Phim modelInsert = new Phim();
                modelInsert.TenPhim = phim.TenPhim;
                modelInsert.HinhAnh = phim.HinhAnh;
                //DateTime temp;
                //try
                //{
                //    try
                //    {
                //        temp = DateTimes.ConvertDate(phim.NgayKhoiChieu);
                //    }
                //    catch (Exception ex)
                //    {
                //        return await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Ngày chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !");
                //    }
                //}
                //catch (Exception ex)
                //{
                //    return await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Ngày khởi chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !");
                //}
                //try
                //{
                //    modelInsert.NgayKhoiChieu = DateTimes.ConvertDate(phim.NgayKhoiChieu);
                //}
                //catch (Exception ex)
                //{
                //    return await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Ngày chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !");
                //}

                if (modelInsert.HinhAnh.Split('.').Count() > 1)
                {
                    modelInsert.HinhAnh = LoaiBoKyTu.bestLower(modelInsert.TenPhim) + "." + modelInsert.HinhAnh.Split('.')[modelInsert.HinhAnh.Split('.').Length - 1];
                }
                else
                {
                    var response = await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Hình ảnh không đúng định dạng!");

                    return(response);
                }
                //Kiểm tra link trailer
                modelInsert.Trailer = phim.Trailer;
                string newString = "";
                if (!string.IsNullOrEmpty(modelInsert.Trailer))
                {
                    newString = modelInsert.Trailer.Replace("https://www.youtube.com/embed/", "♥");
                    if (newString.Split('♥').Length == 1)
                    {
                        return(await thongBaoLoi.thongBaoLoi(ThongBaoLoi.Loi500, "Link trailer không hợp lệ link trailer phải có định dạng: https://www.youtube.com/embed/[thamso]"));
                    }
                }

                var param = new DynamicParameters();
                param.Add("@TENPHIM", modelInsert.TenPhim);
                param.Add("@BIDANH", LoaiBoKyTu.bestLower(phim.TenPhim));
                param.Add("@TRAILER", modelInsert.Trailer);
                param.Add("@HINHANH", hostName + modelInsert.HinhAnh);
                param.Add("@MOTA", phim.MoTa);
                param.Add("@NGAYKHOICHIEU", phim.NgayKhoiChieu);
                param.Add("@DANHGIA", phim.DanhGia);
                param.Add("@DIENVIEN", phim.DienVien);
                param.Add("@DAODIEN", phim.DaoDien);
                param.Add("@DOTUOI", phim.DoTuoi);
                param.Add("@DAXOA", phim.DaXoa);
                param.Add("@KHOICHIEU", phim.KhoiChieu);
                param.Add("@SAPCHIEU", phim.SapChieu);

                connection.Query <Phim>("PHIM_INSERT", param, commandType: CommandType.StoredProcedure);

                return("Thêm phim thành công!");
            }
            //return newString.Split('♥').Length + " " + newString;
        }
        public async Task <ActionResult> CapNhatPhim(PhimInsert model)
        {
            if (string.IsNullOrEmpty(model.NgayKhoiChieu))
            {
                return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Ngày chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !"));
            }
            //dbRapChieuPhimContext dbo = new dbRapChieuPhimContext();
            ////Fixembed
            //var lstPhim = db.Phim;
            //foreach (var item in lstPhim)
            //{
            //    var p = dbo.Phim.Single(n => n.MaPhim == item.MaPhim);
            //    string s = item.Trailer.Split('=')[item.Trailer.Split('=').Length - 1];
            //    p.Trailer = @"https://www.youtube.com/embed/" + s;

            //    dbo.SaveChanges();
            //}
            model.BiDanh = LoaiBoKyTu.bestLower(model.TenPhim);
            try
            {
                Phim phimUpdate = db.Phim.SingleOrDefault(n => n.MaPhim == model.MaPhim);
                if (phimUpdate == null)
                {
                    return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Mã phim không tồn tại!"));
                }
                model.MaNhom = model.MaNhom.ToUpper();
                bool ckb = db.Nhom.Any(n => n.MaNhom == model.MaNhom);
                if (!ckb)
                {
                    return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Mã nhóm không hợp lệ!"));
                }
                string tenPhim = LoaiBoKyTu.bestLower(model.TenPhim);
                if (string.IsNullOrEmpty(tenPhim))
                {
                    return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Tên phim không hợp lệ!"));
                }
                var p = db.Phim.Where(n => n.BiDanh == model.BiDanh);
                if (p.Count() > 2)
                {
                    return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Tên phim đã tồn tại!"));
                }


                phimUpdate.TenPhim = model.TenPhim;
                phimUpdate.BiDanh  = LoaiBoKyTu.bestLower(model.TenPhim);

                phimUpdate.Trailer = model.Trailer;
                phimUpdate.MoTa    = model.MoTa;
                phimUpdate.HinhAnh = model.HinhAnh;
                phimUpdate.DanhGia = model.DanhGia;

                Phim pCu = db.Phim.Where(n => n.MaPhim == model.MaPhim).FirstOrDefault();

                if (string.IsNullOrEmpty(model.HinhAnh))
                {
                    if (pCu != null)
                    {
                        phimUpdate.HinhAnh = pCu.HinhAnh;
                    }
                }

                DateTime temp;
                try
                {
                    try
                    {
                        phimUpdate.NgayKhoiChieu = DateTimes.ConvertDate(model.NgayKhoiChieu);
                    }
                    catch (Exception ex)
                    {
                        return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Ngày chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !"));
                    }
                }
                catch (Exception ex)
                {
                    return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Ngày khởi chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !"));
                }
                if (model.HinhAnh != null)
                {
                    if (model.HinhAnh.Split('.').Count() > 1)
                    {
                        phimUpdate.HinhAnh = LoaiBoKyTu.bestLower(phimUpdate.TenPhim) + "_" + LoaiBoKyTu.bestLower(phimUpdate.MaNhom) + "." + phimUpdate.HinhAnh.Split('.')[phimUpdate.HinhAnh.Split('.').Length - 1];
                    }
                    else
                    {
                        var response = await tbl.TBLoi(ThongBaoLoi.Loi500, "Hình ảnh không đúng định dạng!");

                        return(response);
                    }
                }
                if (!string.IsNullOrEmpty(model.Trailer))
                {
                    string newString = phimUpdate.Trailer.Replace("https://www.youtube.com/embed/", "♥");
                    if (newString.Split('♥').Length == 0)
                    {
                        return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Link trailer không hợp lệ link trailer phải có định dạng: https://www.youtube.com/embed/[thamso]"));
                    }
                }
                db.SaveChanges();
                return(Ok(model));
            }
            catch (Exception ex)
            {
                return(await tbl.TBLoi(ThongBaoLoi.Loi500, "Dữ liệu không hợp lệ!"));
            }
        }
        public async Task <IActionResult> ThemPhim(PhimInsert phim)
        {
            var result = await _quanLyPhimRespository.ThemPhim(phim);

            return(Ok(result));
        }
        public async Task <ResponseEntity> ThemPhim(PhimInsert model)
        {
            model.BiDanh = LoaiBoKyTu.bestLower(model.TenPhim);
            try
            {
                model.MaNhom = model.MaNhom.ToUpper();
                bool ckb = db.Nhom.Any(n => n.MaNhom == model.MaNhom);
                if (!ckb)
                {
                    return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, "Ngày không hợp lệ, Ngày có định dạng dd/MM/yyyy !", MessageConstant.MESSAGE_ERROR_400));
                    //return await tbl.TBLoi(ThongBaoLoi.Loi500, "Mã nhóm không hợp lệ!");
                }
                string tenPhim = LoaiBoKyTu.bestLower(model.TenPhim);
                if (string.IsNullOrEmpty(tenPhim))
                {
                    return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, "Tên phim không hợp lệ !", MessageConstant.MESSAGE_ERROR_400));

                    //return await tbl.TBLoi(ThongBaoLoi.Loi500, "Tên phim không hợp lệ!");
                }
                var p = db.Phim.Where(n => n.BiDanh == model.BiDanh);
                if (p.Count() > 1)
                {
                    return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, "Tên phim đã tồn tại !", MessageConstant.MESSAGE_ERROR_500));

                    //return await tbl.TBLoi(ThongBaoLoi.Loi500, "Tên phim đã tồn tại!");
                }


                PhimInsertNew phimNew     = Mapper.Map <PhimInsert, PhimInsertNew>(model);
                Phim          modelInsert = Mapper.Map <PhimInsertNew, Phim>(phimNew);
                modelInsert.BiDanh = LoaiBoKyTu.bestLower(modelInsert.TenPhim);
                //DateTime temp;
                //if (DateTime.TryParse(model.NgayKhoiChieu, out temp))
                //{
                try
                {
                    modelInsert.NgayKhoiChieu = DateTimes.ConvertDate(model.NgayKhoiChieu);
                }
                catch (Exception ex)
                {
                    return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, "Ngày chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy!", MessageConstant.MESSAGE_ERROR_400));

                    //return await tbl.TBLoi(ThongBaoLoi.Loi500, "Ngày chiếu không hợp lệ, Ngày chiếu phải có định dạng dd/MM/yyyy !");
                }
                //}
                //else
                //{

                //}

                if (modelInsert.HinhAnh.Split('.').Count() > 1)
                {
                    modelInsert.HinhAnh = LoaiBoKyTu.bestLower(modelInsert.TenPhim) + "_" + LoaiBoKyTu.bestLower(modelInsert.MaNhom) + "." + modelInsert.HinhAnh.Split('.')[modelInsert.HinhAnh.Split('.').Length - 1];
                }
                else
                {
                    //var response = await tbl.TBLoi(ThongBaoLoi.Loi500, "Hình ảnh không đúng định dạng!");
                    return(new ResponseEntity(StatusCodeConstants.OK, "Hình ảnh không đúng định dạng!", MessageConstant.MESSAGE_SUCCESS_200));
                }
                if (!string.IsNullOrEmpty(modelInsert.Trailer))
                {
                    string newString = modelInsert.Trailer.Replace("https://www.youtube.com/embed/", "♥");
                    if (newString.Split('♥').Length == 0)
                    {
                        //return await tbl.TBLoi(ThongBaoLoi.Loi500, "Link trailer không hợp lệ link trailer phải có định dạng: https://www.youtube.com/embed/[thamso]");
                        return(new ResponseEntity(StatusCodeConstants.OK, "Hình ảnh không đúng định dạng!", MessageConstant.MESSAGE_SUCCESS_200));
                    }
                }
                modelInsert.MaPhim = 0;
                db.Phim.Add(modelInsert);
                db.SaveChanges();

                return(new ResponseEntity(StatusCodeConstants.OK, modelInsert, MessageConstant.MESSAGE_SUCCESS_200));
                //return Ok(modelInsert);
            }
            catch (Exception ex)
            {
                return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, "thuộc tính hinhAnh không đúng định dạng *.jpg, *.png, *.gif!", MessageConstant.MESSAGE_ERROR_500));

                //return await tbl.TBLoi(ThongBaoLoi.Loi500, "thuộc tính hinhAnh không đúng định dạng *.jpg, *.png, *.gif!");
            }
        }