public AnnouncementAssignedAttribute RemoveAttachment(int announcementAssignedAttributeId) { Trace.Assert(Context.PersonId.HasValue); Trace.Assert(Context.SchoolLocalId.HasValue); using (var uow = Update()) { var da = new AnnouncementAssignedAttributeDataAccess(uow); var attribute = da.GetById(announcementAssignedAttributeId); if (!attribute.AttachmentRef.HasValue) { throw new ChalkableException("Attribute has no attachments for remove."); } if (attribute.IsStiAttribute) { var activityAssignedAttribute = ConnectorLocator.ActivityAssignedAttributeConnector.GetAttribute(attribute.SisActivityId.Value, attribute.SisActivityAssignedAttributeId.Value); activityAssignedAttribute.Attachment = null; ConnectorLocator.ActivityAssignedAttributeConnector.Update(attribute.SisActivityId.Value, attribute.SisActivityAssignedAttributeId.Value, activityAssignedAttribute); } attribute.AttachmentRef = null; da.Update(attribute); uow.Commit(); return(attribute); } }
public AnnouncementAssignedAttribute AddAttachment(AnnouncementTypeEnum announcementType, int announcementId, int assignedAttributeId, int attachmentId) { Trace.Assert(Context.PersonId.HasValue); var ann = ServiceLocator.GetAnnouncementService(announcementType).GetAnnouncementById(announcementId); if (!CanAttach(ann)) { throw new ChalkableSecurityException(); } using (var uow = Update()) { var da = new AnnouncementAssignedAttributeDataAccess(uow); var attribute = da.GetById(assignedAttributeId); if (attribute.AttachmentRef > 0) { throw new ChalkableSisException("You can't attach more than one file to an attribute"); } var attDa = new AttachmentDataAccess(uow); var attachment = attDa.GetById(attachmentId); if (attachment.PersonRef != Context.PersonId) { throw new ChalkableSecurityException(); } attribute.AttachmentRef = attachment.Id; attribute.Attachment = attachment; da.Update(attribute); if (announcementType == AnnouncementTypeEnum.Class) { if (!attachment.SisAttachmentId.HasValue) { var attContent = ServiceLocator.AttachementService.GetAttachmentContent(attachment); var stiAtt = ConnectorLocator.AttachmentConnector.UploadAttachment(attachment.Name, attContent.Content).Last(); MapperFactory.GetMapper <Attachment, StiAttachment>().Map(attachment, stiAtt); attDa.Update(attachment); } if (attribute.SisActivityAssignedAttributeId.HasValue) { var stiAttribute = ConnectorLocator.ActivityAssignedAttributeConnector.GetAttribute(attribute.SisActivityId.Value, attribute.SisActivityAssignedAttributeId.Value); MapperFactory.GetMapper <ActivityAssignedAttribute, AnnouncementAssignedAttribute>().Map(stiAttribute, attribute); ConnectorLocator.ActivityAssignedAttributeConnector.Update(stiAttribute.ActivityId, stiAttribute.Id, stiAttribute); } } uow.Commit(); return(attribute); } }
public AnnouncementAssignedAttribute UploadAttachment(AnnouncementTypeEnum announcementType, int announcementId, int assignedAttributeId, byte[] bin, string name) { Trace.Assert(Context.PersonId.HasValue && Context.SchoolLocalId.HasValue); var ann = ServiceLocator.GetAnnouncementService(announcementType).GetAnnouncementById(announcementId); using (var uow = Update()) { var da = new AnnouncementAssignedAttributeDataAccess(uow); var assignedAttribute = da.GetById(assignedAttributeId); if (!CanAttach(ann)) { throw new ChalkableSecurityException(); } if (assignedAttribute.AttachmentRef > 0) { throw new ChalkableSisException("You can't attach more than one file to an attribute"); } var isStiAttribute = assignedAttribute.IsStiAttribute || announcementType == AnnouncementTypeEnum.Class; var attachment = AttachmentService.Upload(name, bin, isStiAttribute, uow, ServiceLocator, ConnectorLocator); assignedAttribute.AttachmentRef = attachment.Id; assignedAttribute.Attachment = attachment; da.Update(assignedAttribute); if (assignedAttribute.IsStiAttribute) { var stiAttribute = ConnectorLocator.ActivityAssignedAttributeConnector.GetAttribute( assignedAttribute.SisActivityId.Value, assignedAttribute.SisActivityAssignedAttributeId.Value); MapperFactory.GetMapper <ActivityAssignedAttribute, AnnouncementAssignedAttribute>().Map(stiAttribute, assignedAttribute); ConnectorLocator.ActivityAssignedAttributeConnector.Update(stiAttribute.ActivityId, stiAttribute.Id, stiAttribute); } uow.Commit(); return(assignedAttribute); } }
public void Edit(AnnouncementTypeEnum announcementType, int announcementId, IList <AssignedAttributeInputModel> inputAttributes) { BaseSecurity.EnsureAdminOrTeacher(Context); Trace.Assert(Context.PersonId.HasValue); Trace.Assert(Context.SchoolLocalId.HasValue); var annoncement = ServiceLocator.GetAnnouncementService(announcementType).GetAnnouncementById(announcementId); // security check if (inputAttributes != null) { DoUpdate(u => { var da = new AnnouncementAssignedAttributeDataAccess(u); var attributesForUpdate = da.GetAttributesByIds(inputAttributes.Select(x => x.Id).ToList()); foreach (var attribute in attributesForUpdate) { var inputAttr = inputAttributes.FirstOrDefault(x => x.Id == attribute.Id); if (inputAttr == null) { continue; } attribute.AnnouncementRef = inputAttr.AnnouncementId; attribute.Name = inputAttr.Name; attribute.Text = inputAttr.Text; attribute.VisibleForStudents = inputAttr.VisibleForStudents; attribute.AttributeTypeId = inputAttr.AttributeTypeId; attribute.AttachmentRef = inputAttr.AttachmentId; } if (!annoncement.IsDraft) { ValidateAttributes(attributesForUpdate); } da.Update(attributesForUpdate); }); } }