public async Task <ServiceResponse <object> > EditAssignment(int id, AssignmentDtoForEdit assignment) { ClassSectionAssignment dbObj = _context.ClassSectionAssignment.FirstOrDefault(s => s.Id.Equals(id)); if (dbObj != null) { DateTime DueDateTime = DateTime.ParseExact(assignment.DueDateTime, "MM/dd/yyyy", null); var UserObj = _context.Users.Where(m => m.Id == _LoggedIn_UserID).FirstOrDefault(); var SubjectObj = _context.Subjects.Where(m => m.Id == assignment.SubjectId).FirstOrDefault(); var AssignmentName = $"{DateTime.UtcNow.ToShortDateString()} - {UserObj?.FullName} - {SubjectObj?.Name}"; dbObj.AssignmentName = AssignmentName; dbObj.Details = assignment.Details; dbObj.ClassSectionId = assignment.ClassSectionId; dbObj.ReferenceUrl = assignment.ReferenceUrl; dbObj.IsPosted = assignment.IsPosted; dbObj.DueDateTime = DueDateTime; dbObj.SubjectId = assignment.SubjectId; if (assignment.files != null && assignment.files.Count() > 0) { for (int i = 0; i < assignment.files.Count(); i++) { var dbPath = _filesRepository.SaveFile(assignment.files[i]); if (string.IsNullOrEmpty(dbObj.RelatedMaterial)) { dbObj.RelatedMaterial = dbObj.RelatedMaterial + dbPath; } else { dbObj.RelatedMaterial = dbObj.RelatedMaterial + "||" + dbPath; } if (string.IsNullOrEmpty(dbObj.FileName)) { dbObj.FileName = dbObj.FileName + assignment.files[i].FileName + ",," + dbPath; } else { dbObj.FileName = dbObj.FileName + "||" + assignment.files[i].FileName + ",," + dbPath; } } } _context.ClassSectionAssignment.Update(dbObj); await _context.SaveChangesAsync(); } _serviceResponse.Success = true; _serviceResponse.Message = CustomMessage.Updated; return(_serviceResponse); }
public async Task <ServiceResponse <object> > AddAssignment(AssignmentDtoForAdd assignment) { DateTime DueDateTime = DateTime.ParseExact(assignment.DueDateTime, "MM/dd/yyyy", null); var UserObj = _context.Users.Where(m => m.Id == _LoggedIn_UserID).FirstOrDefault(); var SubjectObj = _context.Subjects.Where(m => m.Id == assignment.SubjectId).FirstOrDefault(); var AssignmentName = $"{DateTime.UtcNow.ToShortDateString()} - {UserObj?.FullName} - {SubjectObj?.Name}"; var objToCreate = new ClassSectionAssignment { AssignmentName = AssignmentName, Details = assignment.Details, ClassSectionId = assignment.ClassSectionId, SubjectId = assignment.SubjectId, ReferenceUrl = assignment.ReferenceUrl, DueDateTime = DueDateTime, IsPosted = assignment.IsPosted, SchoolBranchId = _LoggedIn_BranchID, CreatedById = _LoggedIn_UserID, CreatedDateTime = DateTime.UtcNow, }; if (assignment.files != null && assignment.files.Count() > 0) { for (int i = 0; i < assignment.files.Count(); i++) { var dbPath = _filesRepository.SaveFile(assignment.files[i]); if (string.IsNullOrEmpty(objToCreate.RelatedMaterial)) { objToCreate.RelatedMaterial += dbPath; } else { objToCreate.RelatedMaterial = objToCreate.RelatedMaterial + "||" + dbPath; } if (string.IsNullOrEmpty(objToCreate.FileName)) { objToCreate.FileName = objToCreate.FileName + assignment.files[i].FileName + ",," + dbPath; } else { objToCreate.FileName = objToCreate.FileName + "||" + assignment.files[i].FileName + ",," + dbPath; } } } await _context.ClassSectionAssignment.AddAsync(objToCreate); await _context.SaveChangesAsync(); List <Notification> NotificationsToAdd = new List <Notification>(); var ToUsers = (from csUser in _context.ClassSectionUsers join u in _context.Users on csUser.UserId equals u.Id where csUser.ClassSectionId == objToCreate.ClassSectionId && csUser.SchoolBranchId == _LoggedIn_BranchID && u.Role == Enumm.UserType.Student.ToString() select csUser.UserId).ToList(); foreach (var UserId in ToUsers) { NotificationsToAdd.Add(new Notification { Description = GenericFunctions.NotificationDescription(new string[] { "Assignment:", "You have been assigned a new assignment", SubjectObj?.Name, DueDateTime.ToShortDateString() }, UserObj?.FullName), CreatedById = _LoggedIn_UserID, CreatedDateTime = DateTime.UtcNow, IsRead = false, UserIdTo = UserId }); } await _context.Notifications.AddRangeAsync(NotificationsToAdd); await _context.SaveChangesAsync(); _serviceResponse.Success = true; _serviceResponse.Message = CustomMessage.Added; return(_serviceResponse); }