Exemplo n.º 1
0
        public ActionResult <Reminder> PostReminder(ReminderDTO reminderDTO)
        {
            User     user           = _userRepository.GetBy(User.Identity.Name);
            Reminder createReminder = new Reminder(reminderDTO.Title, reminderDTO.DatumReleased, user, reminderDTO.Link, reminderDTO.Description, reminderDTO.Watched);

            foreach (var tag in reminderDTO.Tags)
            {
                Tag createTag = _tagRepository.GetByName(tag.Name);
                if (createTag == null)
                {
                    createTag = new Tag(tag.Name, tag.Color, user);
                    ReminderTag reminderTag = new ReminderTag(createReminder, createTag);
                    createReminder.AddTag(reminderTag, createTag);
                    _tagRepository.Add(createTag);
                }
                else
                {
                    ReminderTag reminderTag = new ReminderTag(createReminder, createTag);
                    createReminder.AddTag(reminderTag, createTag);
                }
            }
            foreach (var checklist in reminderDTO.CheckList)
            {
                ChecklistHeader createChecklistHeader = new ChecklistHeader(checklist.Title, checklist.Volgorde, createReminder, checklist.Finished, checklist.Checked);

                foreach (var item in checklist.Items)
                {
                    ChecklistItem createChecklistItem = new ChecklistItem(item.Title, createChecklistHeader, item.Volgorde, item.Finished, item.Checked);
                    createChecklistHeader.AddItem(createChecklistItem);
                }
                createReminder.AddToCheckList(createChecklistHeader);
            }
            createReminder.RecalculateProcessBar();
            _reminderRepository.Add(createReminder);
            _tagRepository.SaveChanges();
            _reminderRepository.SaveChanges();
            return(CreatedAtAction(nameof(GetReminder), new { id = createReminder.ReminderId }, createReminder));
        }