public IHttpActionResult DiemDanh(LuotThamGiaDto luotThamGiaDto) { if (!ModelState.IsValid) { return(BadRequest()); } var hoatDong = _context.DanhSachHoatDong.Include(hd => hd.DanhSachSinhVienThamGia) .SingleOrDefault(hd => hd.Id == luotThamGiaDto.HoatDongId); if (hoatDong == null) { return(NotFound()); } if (hoatDong.BiHuy) { return(BadRequest("Hoạt động đã bị hủy, không thể điểm danh.")); } if (!hoatDong.DuocPheDuyet) { return(BadRequest("Hoạt động chưa được duyệt, không thể điểm danh.")); } //Check quyền var userSinhVienId = User.Identity.GetSinhVienId(); if (hoatDong.IdSinhVienTaoHd != userSinhVienId && !User.IsInRole("Admin") && !User.IsInRole("QuanLyHoatDong") && !User.IsInRole("DiemDanhHoatDong")) { return(BadRequest("Bạn không có quyền điểm danh hoạt động này.")); } //Lấy sinh viên var sinhVien = _context.SinhVien.SingleOrDefault(sv => sv.Id == luotThamGiaDto.SinhVienId); if (sinhVien == null) { return(NotFound()); } if (sinhVien.LopDangHocId == null) { return(BadRequest("Sinh viên phải đăng kí lớp trước khi tham gia hoạt động")); } var luotThamGia = hoatDong.DanhSachSinhVienThamGia .SingleOrDefault(tghd => tghd.SinhVienId == sinhVien.Id); if (luotThamGia != null) { if (luotThamGia.DuocPheDuyet) { return(BadRequest("Đã điểm danh người này")); } return(BadRequest("Hãy phê duyệt lượt đăng kí của người này ở trên.")); } luotThamGia = new ThamGiaHoatDong(luotThamGiaDto.HoatDongId, sinhVien); hoatDong.ThemLuotThamGia(luotThamGia, sinhVien); _context.SaveChanges(); return(Ok()); }
public IHttpActionResult DangKiThamGia(int hoatDongId) { var hoatDong = _context.DanhSachHoatDong.Include(hd => hd.DanhSachSinhVienThamGia) .Include(hd => hd.DanhSachSinhVienTheoDoi) .SingleOrDefault(hd => hd.Id == hoatDongId); if (hoatDong == null) { return(NotFound()); } if (hoatDong.BiHuy) { return(BadRequest("Hoạt động đã bị hủy, không thể đăng kí tham gia.")); } if (!hoatDong.DuocPheDuyet) { return(BadRequest("Hoạt động chưa được duyệt, không thể đăng kí tham gia.")); } var userId = User.Identity.GetUserId(); var sinhVien = _context.SinhVien.SingleOrDefault(sv => sv.ApplicationUserId == userId); if (sinhVien == null) { return(NotFound()); } if (sinhVien.LopDangHocId == null) { return(BadRequest("Sinh viên phải đăng kí lớp trước khi đăng kí tham gia hoạt động")); } var daCoLuotDangKi = hoatDong.DanhSachSinhVienThamGia.Any(tghd => tghd.SinhVienId == sinhVien.Id); if (daCoLuotDangKi) { return(BadRequest("Bạn đã đăng kí, hãy chờ xác nhận")); } var daCoTheoDoi = hoatDong.DanhSachSinhVienTheoDoi.Any(tghd => tghd.SinhVienId == sinhVien.Id); if (!daCoTheoDoi) { var luotTheoDoi = new TheoDoiHoatDong(hoatDongId, sinhVien.Id); hoatDong.ThemLuotTheoDoi(luotTheoDoi); } var luotDangKi = new ThamGiaHoatDong(hoatDongId, sinhVien, true); hoatDong.ThemLuotDangKi(luotDangKi); _context.SaveChanges(); return(Ok()); }