Ejemplo n.º 1
0
        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);
        }