public JsonResult AddAnnouncements(string _type, string _textArea, string _title) { try { using (ProjeEntities db = new ProjeEntities()) { var user = Session["User"] as Userr; var usc = Session["UserCompany"] as UserCompany; var LAnnouncementType = db.Companies.Include("CompanyAnnouncements").Include("CompanyAnnouncements.Announcement").Where(X => X.Id == usc.Id).ToList(); int typeid = db.Announcements.Where(x => x.Name == _type).Select(z => z.Id).FirstOrDefault(); UserAnnouncement us = new UserAnnouncement(); us.AnnouncementId = typeid; us.UserId = user.Id; us.Details = _textArea; us.Title = _title; db.UserAnnouncements.Add(us); db.SaveChanges(); return(Json("Add")); } } catch (Exception) { throw; } }
public ActionResult Create(Announcement model) { User loggedUser = Session["LoggedUser"] as User; #region Creating announce model.CreateDate = DateTime.Now; model.CreatorId = loggedUser.UserId; model.IsActive = true; _uow.AnnouncementManager.Add(model); if (!_uow.SaveChanges()) { TempData["ProcessResult"] = "An unexpected error occurred while creating the announcement."; TempData["AlertType"] = "danger"; return(RedirectToAction("List")); } #endregion #region Creating announcements for all users in the store List <User> users = _uow.UserManager.ListAll(x => x.IsActive && x.StoreId == loggedUser.StoreId); foreach (var user in users) { UserAnnouncement userAnnouncement = new UserAnnouncement { AnnouncementId = model.AnnouncementId, UserId = user.UserId, IsRead = false }; _uow.UserAnnouncementManager.Add(userAnnouncement); } if (_uow.SaveChanges()) { TempData["ProcessResult"] = "Announcement created and successfully sent to all users."; TempData["AlertType"] = "success"; return(RedirectToAction("List")); } else { TempData["ProcessResult"] = "An unexpected error occurred while sending the announcement to all users."; TempData["AlertType"] = "danger"; return(RedirectToAction("List")); } #endregion }
public async Task Update(UserAnnouncement announcement, CancellationToken ct) { var filter = Builders <UserAnnouncement> .Filter.Eq("_id", announcement.UserId); var user = _announcement.Find(filter).FirstOrDefault(); if (user != null) { var results = await _announcement.ReplaceOneAsync(filter, announcement); } else { await Create(announcement, ct); } }
public async Task <IActionResult> ReadAnnouncement(int annountId) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } var announcement = _announcementService.GetDetail(annountId); var userId = (await _userManager.FindByNameAsync(User.Identity.Name)).Id; _announcementService.UpdateUserAnnouncement(announcement.Id, userId); _announcementService.Save(); var userAnnoun = new UserAnnouncement() { Announcement = announcement, HasRead = true, DateSent = _announcementService.GetDateSend(announcement.Id, userId), }; return(View(userAnnoun)); }
public async Task <IActionResult> Announcement(int?pageSize, int page = 1) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } var userId = (await _userManager.FindByNameAsync(User.Identity.Name)).Id; var announcements = _announcementService.GetUserAnnouncement(userId); var listAnnouns = new List <UserAnnouncement>(); foreach (var announ in announcements) { var userAnnoun = new UserAnnouncement() { Announcement = announ, HasRead = _announcementService.GetUserAnnouncementStatus(announ.Id, userId), DateSent = _announcementService.GetDateSend(announ.Id, userId), }; listAnnouns.Add(userAnnoun); } var sorted = listAnnouns.OrderByDescending(x => x.DateSent); pageSize = _configuration.GetValue <int>("PageSizeAnnouncement"); var results = sorted.Skip((page - 1) * pageSize.Value).Take(pageSize.Value); var pagination = new PageResult <UserAnnouncement>() { CurentPage = page, PageSize = pageSize.Value, Results = results.ToList(), RowCount = sorted.Count() }; return(View(pagination)); }
public async Task <UserAnnouncement> Create(UserAnnouncement announcement, CancellationToken ct) { await _announcement.InsertOneAsync(announcement, null, ct); return(announcement); }