コード例 #1
0
        public IActionResult Post([FromBody] NotePost obj)
        {
            var note = SetTagAndLecture(obj);



            var user     = databaseUser.Read(obj.OwnerId);
            var userNote = new UserNote
            {
                User    = user,
                Note    = note,
                IsOwner = true
            };


            note.Users.Add(userNote);
            user.Notes.Add(userNote);

            var result = databaseNote.Create(note);



            if (result != null)
            {
                return(Ok(new NotePost(note)));
            }
            else
            {
                return(BadRequest(new { error = "User doesn't exist" }));
            }
        }
コード例 #2
0
        private Note ParseToNewNote(NotePost notePost)
        {
            Note note = new Note()
            {
                Title   = notePost.Title,
                Content = notePost.Content,
                Color   = notePost.Color,


                Date = DateTime.Now
            };

            if (notePost.Tag != null)
            {
                note.Tag = databaseTag.Read(Int32.Parse(notePost.Tag));
            }

            if (notePost.Lecture != null)
            {
                note.Lecture = databaseLecture.Read(Int32.Parse(notePost.Lecture));
            }

            if (notePost.Id.HasValue)
            {
                note.Id = notePost.Id.Value;
            }

            return(note);
        }
コード例 #3
0
ファイル: NotesServiceTest.cs プロジェクト: Seko47/DataDrive
        public async void Returns_OkObjectResult200AndCreatedNote_when_NoteCreated()
        {
            IDatabaseContext    databaseContext     = DatabaseTestHelper.GetContext();
            MapperConfiguration mapperConfiguration = new MapperConfiguration(conf =>
            {
                conf.AddProfile(new Note_to_NoteOut());
            });
            IMapper mapper = mapperConfiguration.CreateMapper();

            INotesService notesService = new NotesService(databaseContext, mapper);

            NotePost newNotePost = new NotePost
            {
                Title   = "Note's title",
                Content = "Note's content"
            };

            StatusCode <NoteOut> status = await notesService.PostNoteByUser(newNotePost, ADMIN_USERNAME);

            Assert.NotNull(status);
            Assert.Equal(StatusCodes.Status200OK, status.Code);
            Assert.NotNull(status.Body);
            Assert.Equal(newNotePost.Title, status.Body.Title);
            Assert.Equal(newNotePost.Content, status.Body.Content);
            Assert.Equal(DAO.Models.Base.ResourceType.NOTE, status.Body.ResourceType);
            Assert.True(databaseContext.Notes.AnyAsync(_ => _.ID == status.Body.ID).Result);
        }
コード例 #4
0
        public async void Returns_NoteOut_when_Success()
        {
            NotePost notePost = new NotePost
            {
                Title   = "New note's title",
                Content = "New note's content"
            };

            NoteOut noteOut = new NoteOut
            {
                ID      = Guid.NewGuid(),
                Title   = "New note's title",
                Content = "New note's content"
            };

            StatusCode <NoteOut> status           = new StatusCode <NoteOut>(StatusCodes.Status200OK, noteOut);
            Mock <INotesService> notesServiceMock = new Mock <INotesService>();

            notesServiceMock.Setup(_ => _.PostNoteByUser(It.IsAny <NotePost>(), It.IsAny <string>()))
            .Returns(Task.FromResult(status));

            NotesController notesController = new NotesController(notesServiceMock.Object, UserManagerHelper.GetUserManager(ADMIN_USERNAME));

            notesController.Authenticate(ADMIN_USERNAME);

            IActionResult result = await notesController.Post(notePost);

            OkObjectResult okObjectResult = result as OkObjectResult;

            Assert.NotNull(okObjectResult);
            Assert.IsType <NoteOut>(okObjectResult.Value);
        }
コード例 #5
0
        private Note Parse(NotePost notePost)
        {
            Note parser = new Note()
            {
                Title   = notePost.Title,
                Content = notePost.Content,
                Color   = notePost.Color,


                Date = DateTime.Now
            };

            return(parser);
        }
コード例 #6
0
		public object Deserialize(JsonValue json, JsonMapper mapper)
		{
			NotePost post = null;
			if ( json != null && !json.IsNull )
			{
				post = new NotePost();
				post.ID          = json.ContainsName("id"          ) ? json.GetValue<string>("id"     ) : String.Empty;
				post.Subject     = json.ContainsName("subject"     ) ? json.GetValue<string>("subject") : String.Empty;
				post.CreatedTime = json.ContainsName("created_time") ? JsonUtils.ToDateTime(json.GetValue<string>("created_time"), "yyyy-MM-ddTHH:mm:ss") : DateTime.MinValue;
				post.UpdatedTime = json.ContainsName("updated_time") ? JsonUtils.ToDateTime(json.GetValue<string>("updated_time"), "yyyy-MM-ddTHH:mm:ss") : DateTime.MinValue;
				
				post.From        = mapper.Deserialize<Reference>(json.GetValue("from"));
			}
			return post;
		}
        public object Deserialize(JsonValue json, JsonMapper mapper)
        {
            NotePost post = null;

            if (json != null && !json.IsNull)
            {
                post             = new NotePost();
                post.ID          = json.ContainsName("id") ? json.GetValue <string>("id") : String.Empty;
                post.Subject     = json.ContainsName("subject") ? json.GetValue <string>("subject") : String.Empty;
                post.CreatedTime = json.ContainsName("created_time") ? JsonUtils.ToDateTime(json.GetValue <string>("created_time"), "yyyy-MM-ddTHH:mm:ss") : DateTime.MinValue;
                post.UpdatedTime = json.ContainsName("updated_time") ? JsonUtils.ToDateTime(json.GetValue <string>("updated_time"), "yyyy-MM-ddTHH:mm:ss") : DateTime.MinValue;

                post.From = mapper.Deserialize <Reference>(json.GetValue("from"));
            }
            return(post);
        }
コード例 #8
0
        public IActionResult Put(int userId, [FromBody] NotePost obj)
        {
            var note = SetTagAndLecture(obj);



            var result = databaseNote.Update(note);

            if (result != null)
            {
                return(Ok(new NotePost(note)));
            }
            else
            {
                return(BadRequest(new { error = "Note doesn't exist" }));
            }
        }
コード例 #9
0
        private List <User> ShareNotes(NotePost notePost)
        {
            IList <User> userList    = databaseUser.Read();
            List <User>  newUserList = new List <User>();

            foreach (User us in userList)
            {
                for (int i = 0; i < notePost.SharedTo.Count; i++)
                {
                    if (us.Id == notePost.SharedTo[i])
                    {
                        newUserList.Add(us);
                    }
                }
            }
            return(newUserList);
        }
コード例 #10
0
        private Note SetTagAndLecture(NotePost obj)
        {
            var tag     = new List <Tag>(databaseTag.Read()).Find(x => x.Name == obj.Tag);
            var lecture = new List <Lecture>(databaseLecture.Read()).Find(x => x.Name == obj.Lecture);

            var note = Parse(obj);

            if (obj.Id.HasValue)
            {
                note.Id = obj.Id.Value;
            }

            if (tag == null && !String.IsNullOrWhiteSpace(obj.Tag))
            {
                tag = new Tag()
                {
                    Name = obj.Tag
                };
                databaseTag.Create(tag);
                note.Tag = tag;
            }

            if (tag != null && !String.IsNullOrWhiteSpace(obj.Tag))
            {
                note.AddTag(tag);
            }

            if (lecture == null && !String.IsNullOrWhiteSpace(obj.Lecture))
            {
                lecture = new Lecture()
                {
                    Name = obj.Lecture
                };
                databaseLecture.Create(lecture);
                note.Lecture = lecture;
            }

            if (tag != null && !String.IsNullOrWhiteSpace(obj.Lecture))
            {
                note.AddLecture(lecture);
            }

            return(note);
        }
コード例 #11
0
ファイル: NotesService.cs プロジェクト: Seko47/DataDrive
        public async Task <StatusCode <NoteOut> > PostNoteByUser(NotePost note, string username)
        {
            string userId = (await _databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == username))?
                            .Id;

            Note newNote = new Note
            {
                CreatedDateTime = DateTime.Now,
                ResourceType    = DAO.Models.Base.ResourceType.NOTE,
                Content         = note.Content,
                OwnerID         = userId,
                Title           = note.Title
            };

            await _databaseContext.Notes.AddAsync(newNote);

            await _databaseContext.SaveChangesAsync();

            NoteOut result = _mapper.Map <NoteOut>(newNote);

            return(new StatusCode <NoteOut>(StatusCodes.Status200OK, result));
        }
コード例 #12
0
        public async Task <IActionResult> Post([FromBody] NotePost notePost)
        {
            StatusCode <NoteOut> status = await _notesService.PostNoteByUser(notePost, _userManager.GetUserName(User));

            return(Ok(status.Body));
        }