Ejemplo n.º 1
0
 public IActionResult AddLabelToANote(int noteID, AddLabelNoteRequest addLabelNote)
 {
     try
     {
         bool   success = false;
         string message;
         var    idClaim            = HttpContext.User.Claims.FirstOrDefault(id => id.Type.Equals("id", StringComparison.InvariantCultureIgnoreCase));
         int    userID             = Convert.ToInt32(idClaim.Value);
         UserNoteResponseData data = _userNoteBusiness.AddlabelsToNote(userID, noteID, addLabelNote);
         if (data != null)
         {
             success = true;
             message = "Label Added to note Succesfully";
             return(Ok(new { success, message, data }));
         }
         else
         {
             message = "Try Again!";
             return(Ok(new { success, message, data }));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(new { ex.Message }));
     }
 }
        public async Task <IActionResult> AddlabelsToNote(int NoteId, AddLabelNoteRequest addLabelNote)
        {
            try
            {
                var    user   = HttpContext.User;
                bool   status = false;
                string message;
                if (user.HasClaim(c => c.Type == _tokenType))
                {
                    if (user.Claims.FirstOrDefault(c => c.Type == _tokenType).Value == _login &&
                        user.Claims.FirstOrDefault(c => c.Type == _userType).Value == _regularUser)
                    {
                        int UserId = Convert.ToInt32(user.Claims.FirstOrDefault(c => c.Type == _userId).Value);

                        NoteResponseModel data = await _notesBusiness.AddlabelsToNote(NoteId, UserId, addLabelNote);

                        if (data != null)
                        {
                            status  = true;
                            message = "Labels Updated Successfull";
                            return(Ok(new { status, message, data }));
                        }
                        message = "Labels Cannot be Updated.";
                        return(Ok(new { status, message }));
                    }
                }
                message = "Invalid Token.";
                return(BadRequest(new { status, message }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }
        public UserNoteResponseData AddlabelsToNote(int userID, int noteID, AddLabelNoteRequest addLabelNote)
        {
            try
            {
                List <NotesLabel> labels = _context.NotesLabels.Where(notes => notes.NotesId == noteID).ToList();

                if (labels != null && labels.Count != 0)
                {
                    _context.NotesLabels.RemoveRange(labels);
                    _context.SaveChanges();
                }

                if (addLabelNote.Label.Count > 0)
                {
                    List <NotesLabelRequest> labelRequests = addLabelNote.Label;
                    foreach (NotesLabelRequest labelRequest in labelRequests)
                    {
                        LabelInfo labelInfo = _context.Labels.
                                              Where(labeled => labeled.UserID == userID && labeled.LabelID == labelRequest.LabelId).
                                              FirstOrDefault <LabelInfo>();

                        if (labelRequest.LabelId > 0 && labelInfo != null)
                        {
                            var data = new NotesLabel
                            {
                                LabelId      = labelRequest.LabelId,
                                NotesId      = noteID,
                                CreatedDate  = DateTime.Now,
                                ModifiedDate = DateTime.Now
                            };

                            _context.NotesLabels.Add(data);
                            _context.SaveChanges();
                        }
                    }
                }

                var notesinfo = _context.UserNotes.
                                Where(note => note.NotesId == noteID && note.UserId == userID).
                                First <UserNotesInfo>();
                List <LabelResponseData> labelsData = _context.NotesLabels.
                                                      Where(note => note.NotesId == notesinfo.NotesId).
                                                      Join(_context.Labels,
                                                           noteLabel => noteLabel.LabelId,
                                                           label => label.LabelID,
                                                           (noteLabel, label) => new LabelResponseData
                {
                    LabelID   = noteLabel.LabelId,
                    LabelName = label.LabelName,
                }).
                                                      ToList();

                UserNoteResponseData noteResponseData = new UserNoteResponseData()
                {
                    NoteId      = notesinfo.NotesId,
                    Title       = notesinfo.Title,
                    Description = notesinfo.Description,
                    Color       = notesinfo.Color,
                    Image       = notesinfo.Image,
                    Pin         = notesinfo.Pin,
                    Archived    = notesinfo.Archived,
                    Trash       = notesinfo.Trash,
                    Reminder    = notesinfo.Reminder,
                    Labels      = labelsData
                };

                return(noteResponseData);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 /// <summary>
 /// Update Labels to Notes
 /// </summary>
 /// <param name="NoteId"> NoteId </param>
 /// <param name="addLabelNote">Add Label To Note</param>
 /// <returns></returns>
 public async Task <NoteResponseModel> AddlabelsToNote(int NoteId, int UserId, AddLabelNoteRequest addLabelNote)
 {
     try
     {
         if (NoteId <= 0 || addLabelNote == null)
         {
             return(null);
         }
         else
         {
             return(await _notesRepository.AddlabelsToNote(NoteId, UserId, addLabelNote));
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
        public UserNoteResponseData AddlabelsToNote(int userID, int noteID, AddLabelNoteRequest addLabelNote)
        {
            UserNoteResponseData data = _userNoteRepository.AddlabelsToNote(userID, noteID, addLabelNote);

            return(data);
        }