public async Task <IActionResult> UpdateNote([FromBody] NotepadModel note)
        {
            string    token = Request.Headers["Authorization"].ToString().Substring("Bearer ".Length).Trim();
            UserModel user  = await _jwtTokenService.GetUserFromTokenStrAsync(token);

            await _notepadService.UpdateNoteAsync(user, note);

            return(Ok());
        }
        //User updates note
        public async Task UpdateNoteAsync(UserModel user, NotepadModel note)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            if (note.Id == 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Id Not Exist", "Notepad id field is required");
                errors.Throw();
            }

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            NotepadModel old = await _notepadRepository.GetNoteByIdAsync(note.Id, true);

            if (old == null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Note Not Exist", "Note is not exist");
                errors.Throw();
            }

            if (old.Id != note.Id)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Note Not Belongs Home", "Note does not belong this home");
                errors.Throw();
            }

            old.Title   = note.Title;
            old.Content = note.Content;

            await _notepadRepository.UpdateAsync(old);

            foreach (var friend in home.Users)
            {
                FCMModel fcm = new FCMModel(friend.DeviceId, type: "NotepadUpdate");
                fcm.data.Add("UpdatedNote", old);
                await _fcmService.SendFCMAsync(fcm);
            }
        }
예제 #3
0
        public IActionResult Change(NotepadModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values.SelectMany(e => e.Errors)));
            }

            var contact = new Contact(model.ID);

            contact.Name         = model.Name;
            contact.PhoneNumbers = model.GetPhoneNumbers(contact, _context);
            contact.Emails       = model.GetEmails(contact, _context);

            model.RemoveEmails(_context);
            model.RemovePhoneNumbers(_context);

            _context.Set <Contact>().Update(contact);
            _context.SaveChanges();


            return(Ok());
        }
        //User deletes note
        public async Task DeleteNoteAsync(UserModel user, int noteId)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            NotepadModel note = await _notepadRepository.GetNoteByIdAsync(noteId, true);

            if (note == null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Note Not Exist", "Note is not exist");
                errors.Throw();
            }

            if (note.Home != home)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Note Not Belongs Home", "Note does not belong this home");
                errors.Throw();
            }

            foreach (var friend in home.Users)
            {
                FCMModel fcm = new FCMModel(friend.DeviceId, type: "NotepadDelete");
                fcm.data.Add("DeletedNote", note.Id);
                await _fcmService.SendFCMAsync(fcm);
            }

            await _notepadRepository.DeleteAsync(note);
        }
예제 #5
0
        public IActionResult Register(NotepadModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values.SelectMany(e => e.Errors)));
            }

            if (!string.IsNullOrEmpty(model.Name))
            {
                var newContact = new Contact()
                {
                    Name = model.Name
                };
                _context.Set <Contact>().Add(newContact);

                foreach (PhoneNumber phoneNumber in model.PhoneNumbers)
                {
                    _context.Set <PhoneNumber>().Add(new PhoneNumber()
                    {
                        Number  = phoneNumber.Number,
                        Contact = newContact
                    });
                }

                foreach (Email email in model.Emails)
                {
                    _context.Set <Email>().Add(new Email()
                    {
                        EmailName = email.EmailName,
                        Contact   = newContact
                    });
                }

                _context.SaveChanges();
            }

            return(Ok());
        }
        //User adds note
        public async Task AddNoteAsync(UserModel user, NotepadModel note)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            note.Home = home;

            await _notepadRepository.InsertAsync(note);

            foreach (var friend in home.Users)
            {
                FCMModel fcm = new FCMModel(friend.DeviceId, type: "NotepadAdd");
                fcm.data.Add("NewNote", note);
                await _fcmService.SendFCMAsync(fcm);
            }
        }
예제 #7
0
 public Service1()
 {
     model = new NotepadModel();
 }