Exemple #1
0
        public void DangTin(BanTin banTin, string noiDungTuKhoa)
        {
            if (banTin == null)
            {
                throw new ArgumentNullException("banTin", "Bản tin rỗng");
            }

            if (string.IsNullOrWhiteSpace(banTin.NoiDung))
            {
                throw new Exception("Bản tin phải có nội dung");
            }

            try
            {
                var thanhVien = ThanhVienRepository.Get(banTin.ThanhVienId);
                if (thanhVien == null)
                {
                    throw new Exception("Thành viên không tồn tại");
                }

                TuKhoa tuKhoa = null;
                if (!string.IsNullOrWhiteSpace(noiDungTuKhoa))
                {
                    noiDungTuKhoa = noiDungTuKhoa.Trim();
                    var tk = TuKhoaRepository.GetTuKhoaByNoiDung(noiDungTuKhoa);
                    if (tk == null)
                    {
                        tuKhoa         = new TuKhoa();
                        tuKhoa.NoiDung = noiDungTuKhoa;
                        TuKhoaRepository.Create(tuKhoa);
                        TuKhoaRepository.SaveChanges();
                    }
                    else
                    {
                        tuKhoa = tk;
                    }
                }

                banTin.ThoiGian = DateTime.Now;

                BanTinRepository.Create(banTin);
                BanTinRepository.SaveChanges();

                if (tuKhoa != null && tuKhoa.TuKhoaId > 0)
                {
                    var banTinTuKhoa = new BanTinTuKhoa
                    {
                        BanTinId = banTin.BanTinId,
                        TuKhoaId = tuKhoa.TuKhoaId
                    };
                    BanTinTuKhoaRepository.Create(banTinTuKhoa);
                }

                BanTinTuKhoaRepository.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Chúng tôi đang gặp vấn đề khó về kỹ thuật khi đăng tin", ex);
            }
        }
        public void DangTuKhoa(BanTinTuKhoa banTinTK, TuKhoa tuKhoa)
        {
            if (banTinTK == null)
            {
                throw new ArgumentNullException("tuKhoa", "Tu Khoa rỗng");
            }

            var banTin = BanTinStore.Get(banTinTK.BanTinId);

            if (banTin == null)
            {
                throw new Exception("Ban tin không tồn tại");
            }

            var bantinTuKhoa = TuKhoaStore.Get(banTinTK.TuKhoaId);

            if (tuKhoa == null)
            {
                throw new Exception("Tu khoa khong ton tai");
            }
            if (string.IsNullOrWhiteSpace(tuKhoa.NoiDung))
            {
                throw new Exception("tu khoa phải có nội dung");
            }

            banTinTK.TuKhoaId = 1;

            try
            {
                BanTinTuKhoaStore.Create(banTinTK);
                BanTinTuKhoaStore.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Chúng tôi đang gặp vấn đề khó về kỹ thuật khi đăng tin", ex);
            }
        }
        public BanTinTuKhoa SuaTuKhoa(BanTinTuKhoa banTinTK)
        {
            if (banTinTK == null)
            {
                throw new ArgumentNullException("banTin", "Bản tin rỗng");
            }

            var banTin = BanTinStore.Get(banTinTK.BanTinId);

            if (banTin == null)
            {
                throw new Exception("Ban tin không tồn tại");
            }

            var tuKhoaDb = TuKhoaStore.Get(banTinTK.TuKhoaId);

            if (banTinTK == null)
            {
                throw new Exception("Tu khoa khong ton tai");
            }

            //if (banTinTK.ThanhVienId != tuKhoaDb.ThanhVienId)
            //    throw new Exception("Bạn không có quyền sửa bản tin này");

            // TODO: Cần lưu lại lịch sử sửa bản tin
            try
            {
                BanTinTuKhoaStore.Create(banTinTK);
                BanTinTuKhoaStore.SaveChanges();
                return(banTinTK);
            }
            catch (Exception ex)
            {
                throw new Exception("Chúng tôi đang gặp vấn đề khó về kỹ thuật khi đăng tin", ex);
            }
        }
 public void Create(BanTinTuKhoa banTinTK)
 {
     Db.BanTinTuKhoas.Add(banTinTK);
 }