public ActionResult DemoAddScheduleSecond(FormCollection form)
        {
            var maLopHocPhan   = form["maLopHocPhan"];
            var tietHocBatDau  = form["tietHocBatDau"];
            var tietHocKetThuc = form["tietHocKetThuc"];
            var ngayHoc        = form["ngayHoc"];
            var phongHoc       = form["phongHoc"];

            var lichHoc = new LICHHOC()
            {
                MaLopHocPhan   = maLopHocPhan,
                PhongHoc       = int.Parse(phongHoc),
                TietHocBatDau  = int.Parse(tietHocBatDau),
                TietHocKetThuc = int.Parse(tietHocKetThuc),
                NgayHoc        = DateTime.ParseExact(ngayHoc, "yyyy-MM-dd", CultureInfo.InvariantCulture)
            };

            DemoKetQua demo = new DemoKetQua();

            if (LopHocPhanDao.TonTaiLichHoc(lichHoc))
            {
                demo.PhanHoi = "Lịch học đã tồn tại";
                return(RedirectToAction("DemoAddScheduleFirst", "Demo", demo));
            }

            LopHocPhanDao.AddLichHoc(lichHoc);
            FCMController fcm          = new FCMController();
            string        notification = fcm.CreateScheduleNotification(lichHoc);
            string        response     = fcm.SendMessage(notification);

            demo.PhanHoi  = response;
            demo.TinGuiDi = notification;

            return(RedirectToAction("DemoAddScheduleFirst", "Demo", demo));
        }
        public ActionResult DemoPostNews(FormCollection form)
        {
            string   tieuDe       = HttpUtility.HtmlEncode(form["txtTieuDe"]);
            string   noiDung      = HttpUtility.HtmlEncode(form["txtNoiDung"]);
            DateTime thoiGianDang = DateTime.Now;

            // Them vao db
            THONGBAO thongBao = new THONGBAO()
            {
                MaThongBao   = 1,
                TieuDe       = tieuDe,
                NoiDung      = noiDung,
                ThoiGianDang = thoiGianDang
            };

            DemoKetQua demo = new DemoKetQua();

            try
            {
                ThongBaoDao.AddThongBao(thongBao);
                thongBao.MaThongBao = ThongBaoDao.GetMaxId();

                // Thong bao den client app
                FCMController fcm      = new FCMController();
                string        message  = fcm.CreateNewsNotification(thongBao);
                string        response = fcm.SendMessage(message);

                // Hien thi ket qua thanh cong
                demo.KetQua   = "Them thanh cong !!!";
                demo.PhanHoi  = response;
                demo.TinGuiDi = message;
            }
            catch (Exception ex)
            {
                // hien thi loi
                demo.PhanHoi = ex.Message + ex.StackTrace;
            }

            return(RedirectToAction("DemoPostNews", "Demo", demo));
        }
        public IHttpActionResult TraLoiTinNhan(string maSinhVien, string matKhau, TinNhan tinNhan)
        {
            if (!SinhVienDao.TonTaiSinhVien(maSinhVien, matKhau))
            {
                return(BadRequest("Thông tin người gửi không đúng"));
            }

            var hoTenNguoiGui = tinNhan.HoTenNguoiGui;
            var maxMaTinNhan  = TinNhanDao.GetMaxMaTinNhan() + 1;

            TINNHAN newTinNhan = new TINNHAN()
            {
                MaTinNhan   = maxMaTinNhan,
                TieuDe      = tinNhan.TieuDe,
                NoiDung     = tinNhan.NoiDung,
                ThoiDiemGui = DateTime.Now
            };

            NGUOIGUI nguoiGui = new NGUOIGUI()
            {
                MaTinNhan     = maxMaTinNhan,
                MaNguoiGui    = tinNhan.MaNguoiGui,
                HoTenNguoiGui = hoTenNguoiGui,
                TINNHAN       = newTinNhan,
                TrangThai     = TinNhanDao.TINNHAN_CHUA_XOA,
                TAIKHOAN      = null
            };

            var nguoiNhans = new List <NGUOINHAN>();

            foreach (var item in tinNhan.NguoiNhans)
            {
                NGUOINHAN nguoiNhan = new NGUOINHAN()
                {
                    MaTinNhan      = maxMaTinNhan,
                    HoTenNguoiNhan = SinhVienDao.GetHoTenTheoTaiKhoan(item.MaNguoiNhan),
                    ThoiDiemXem    = null,
                    TINNHAN        = newTinNhan,
                    TAIKHOAN       = null,
                    TrangThai      = TinNhanDao.TINNHAN_CHUA_XOA,
                    MaNguoiNhan    = item.MaNguoiNhan
                };
                nguoiNhans.Add(nguoiNhan);
            }

            newTinNhan.NGUOIGUIs = new List <NGUOIGUI> {
                nguoiGui
            };
            newTinNhan.NGUOINHANs = nguoiNhans;
            TinNhanDao.AddTinNhan(newTinNhan);

            try
            {
                // Thong bao den client app
                FCMController fcm      = new FCMController();
                string        message  = fcm.CreateMessageNotification(newTinNhan);
                string        response = fcm.SendMessage(message);
                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message + "\n" + ex.InnerException.Message));
            }
        }
        public ActionResult DemoPostMessage(FormCollection form)
        {
            var demo = new DemoKetQua();

            var tieuDe              = HttpUtility.HtmlEncode(form["txtTieuDe"]);
            var noiDung             = HttpUtility.HtmlEncode(form["txtNoiDung"]);
            var maNguoiGui          = form["txtMaNguoiNguoi"];
            var thoiDiemGui         = DateTime.Now;
            var danhSachMaNguoiNhan = form["txtNguoiNhan[]"].Split(',').ToList <string>();

            try
            {
                // Them tin nhan vao db
                TINNHAN tinNhan = new TINNHAN()
                {
                    MaTinNhan   = TinNhanDao.GetMaxMaTinNhan() + 1, // tu dong tang
                    TieuDe      = tieuDe,
                    NoiDung     = noiDung,
                    ThoiDiemGui = thoiDiemGui,
                };

                // Lay danh sach thong tin nguoi nhan
                var danhSachNguoiNhan = new List <NGUOINHAN>();
                danhSachMaNguoiNhan.ForEach(m => danhSachNguoiNhan.Add(
                                                new NGUOINHAN()
                {
                    MaTinNhan      = tinNhan.MaTinNhan,
                    MaNguoiNhan    = int.Parse(m),
                    TrangThai      = TinNhanDao.TINNHAN_CHUA_XOA,
                    HoTenNguoiNhan = SinhVienDao.GetHoTenTheoTaiKhoan(int.Parse(m)),
                    ThoiDiemXem    = null,
                    TINNHAN        = tinNhan,
                    TAIKHOAN       = null
                }
                                                ));

                //// Nguoi gui
                var nguoiGui = new List <NGUOIGUI>
                {
                    new NGUOIGUI()
                    {
                        MaNguoiGui    = int.Parse(maNguoiGui),
                        TrangThai     = TinNhanDao.TINNHAN_CHUA_XOA,
                        MaTinNhan     = tinNhan.MaTinNhan,
                        HoTenNguoiGui = SinhVienDao.GetHoTenTheoTaiKhoan(int.Parse(maNguoiGui)),
                        TINNHAN       = tinNhan,
                        TAIKHOAN      = null
                    }
                };

                tinNhan.NGUOINHANs = danhSachNguoiNhan;
                tinNhan.NGUOIGUIs  = nguoiGui;
                TinNhanDao.AddTinNhan(tinNhan);

                // Thong bao den client app
                FCMController fcm     = new FCMController();
                string        message = fcm.CreateMessageNotification(tinNhan);
                demo.TinGuiDi = message;

                string response = fcm.SendMessage(message);
                demo.PhanHoi = response;

                // Hien thi ket qua thanh cong
                demo.KetQua = "Them thanh cong !!!";
            }
            catch (Exception ex)
            {
                demo.PhanHoi = ex.Message + ex.StackTrace;
            }

            return(RedirectToAction("DemoPostMessage", "Demo", demo));
        }