Example #1
0
        public override void Submit(int announcementId)
        {
            Trace.Assert(Context.SchoolLocalId.HasValue);
            var res = GetAnnouncementDetails(announcementId);

            if (res.ClassAnnouncementData.Title == null || Exists(res.ClassAnnouncementData.Title, res.ClassAnnouncementData.ClassRef, res.ClassAnnouncementData.Expires, res.ClassAnnouncementData.Id))
            {
                throw new ChalkableException("Invalid Class Announcement Title");
            }

            using (var uow = Update())
            {
                var da = CreateClassAnnouncementDataAccess(uow);
                AnnouncementSecurity.EnsureInModifyAccess(res, Context);
                if (res.ClassAnnouncementData.IsDraft)
                {
                    ServiceLocator.AnnouncementAssignedAttributeService.ValidateAttributes(res.AnnouncementAttributes);
                    res.ClassAnnouncementData.State   = AnnouncementState.Created;
                    res.ClassAnnouncementData.Created = Context.NowSchoolTime.Date;

                    var activity = new Activity();
                    MapperFactory.GetMapper <Activity, AnnouncementDetails>().Map(activity, res);
                    activity = ConnectorLocator.ActivityConnector.CreateActivity(res.ClassAnnouncementData.ClassRef, activity);
                    if (da.Exists(activity.Id))
                    {
                        throw new ChalkableException("Announcement with such activityId already exists");
                    }

                    var annAttDa = new AnnouncementAssignedAttributeDataAccess(uow);
                    annAttDa.Delete(res.AnnouncementAttributes);
                    MapperFactory.GetMapper <AnnouncementDetails, Activity>().Map(res, activity);
                    var attributes = res.AnnouncementAttributes.Where(x => x.Attachment != null).ToList();
                    if (attributes.Count > 0)
                    {
                        var atts = new AttachmentDataAccess(uow).GetBySisAttachmentIds(attributes.Select(a => a.Attachment.SisAttachmentId.Value).ToList());
                        foreach (var attribute in res.AnnouncementAttributes)
                        {
                            if (attribute.Attachment == null)
                            {
                                continue;
                            }
                            var att = atts.FirstOrDefault(x => x.SisAttachmentId == attribute.Attachment.SisAttachmentId);
                            if (att == null)
                            {
                                continue;
                            }
                            attribute.AttachmentRef = att.Id;
                        }
                    }
                    annAttDa.Insert(res.AnnouncementAttributes);
                }
                da.Update(res.ClassAnnouncementData);
                uow.Commit();
            }
        }
Example #2
0
 public void Delete(int assignedAttributeId)
 {
     BaseSecurity.EnsureAdminOrTeacher(Context);
     Trace.Assert(Context.PersonId.HasValue);
     Trace.Assert(Context.SchoolLocalId.HasValue);
     DoUpdate(u =>
     {
         var da        = new AnnouncementAssignedAttributeDataAccess(u);
         var attribute = da.GetById(assignedAttributeId);
         if (attribute.SisActivityId.HasValue && attribute.SisActivityAssignedAttributeId.HasValue)
         {
             ConnectorLocator.ActivityAssignedAttributeConnector.Delete(attribute.SisActivityId.Value, attribute.SisActivityAssignedAttributeId.Value);
         }
         da.Delete(assignedAttributeId);
     });
 }