private void MoveToSlot(Employee employee, string attachmentId)
        {
            if (employee.AttachmentsBySlot == null)
            {
                employee.AttachmentsBySlot = new List <SlotWithAttachment>();
            }

            var note = employee.Notes.Where(x => x.Attachment != null && x.Attachment.Id == attachmentId).First();
            var slot = employee.AttachmentsBySlot.Where(x => x.SlotId == SlotId).FirstOrDefault();

            if (slot == null)
            {
                slot = new SlotWithAttachment()
                {
                    SlotId = SlotId
                };
                employee.AttachmentsBySlot.Add(slot);
            }

            if (slot.Attachment == null)
            {
                slot.Date       = note.RegisterDate;
                slot.Attachment = note.Attachment;

                if (string.IsNullOrWhiteSpace(note.Note) || note.Note == DownNoteText)
                {
                    employee.Notes.Remove(note);
                }
                else
                {
                    note.Attachment = null;
                }
            }
        }
Exemple #2
0
        public ActionResult QuickAttachment()
        {
            //The normal binding does not work
            var id            = RouteData.Values["id"] as string;
            var slotId        = Request.Form["slot"] as string;
            var name          = Request.Form["name"] as string;
            var uploadToNotes = string.IsNullOrEmpty(slotId);

            Employee employee;

            if (id == null)
            {
                employee = CreateEmployee(name);
            }
            else
            {
                employee = RavenSession.Load <Employee>(id);
                if (employee == null)
                {
                    return(HttpNotFound());
                }
            }

            using (var attachmentReader = new RequestAttachmentReader(Request))
            {
                var reading = attachmentReader.Select(x => ExecuteCommand(new SaveAttachment(employee, x.Key, x.Value)));
                if (!uploadToNotes)
                {
                    reading = reading.Take(1);
                }
                var attachments          = reading.ToArray();
                SlotWithAttachment added = null;

                if (string.IsNullOrEmpty(slotId))
                {
                    QuickAttachToNotes(employee, attachments);
                }
                else
                {
                    var slot = RavenSession.Load <AttachmentSlot>(slotId);
                    if (slot == null)
                    {
                        return(HttpNotFound());
                    }

                    added = employee.AddAttachment(attachments.First(), slot);
                }

                return(Json(new
                {
                    success = true,
                    entityId = employee.Id,
                    editUrl = Url.Action("Edit", new { id = employee.Id }),
                    attachment = attachments.FirstOrDefault(),
                    attachments = attachments,
                    added = added
                }));
            }
        }