public async Task <NoteDTO> CreateNoteAsync(string userId, int logbookId, string description, string image, int?categoryId) { validator.IsDescriptionIsNullOrEmpty(description); validator.IsDescriptionInRange(description); var user = await this.context.Users.FindAsync(userId); if (user == null) { throw new NotFoundException(ServicesConstants.UserNotFound); } var logbook = await this.context.Logbooks.FindAsync(logbookId); if (logbook == null) { throw new NotFoundException(ServicesConstants.LogbookNotFound); } if (!this.context.UsersLogbooks.Any(x => x.UserId == userId && x.LogbookId == logbookId)) { throw new NotAuthorizedException(string.Format(ServicesConstants.UserNotManagerOfLogbook, user.UserName, logbook.Name)); } var note = new Note() { Description = description, Image = image, CreatedOn = DateTime.Now, NoteCategoryId = categoryId, UserId = userId, LogbookId = logbookId }; if (categoryId != null) { var noteCategory = await this.context.NoteCategories.FindAsync(categoryId); if (noteCategory == null) { throw new NotFoundException(ServicesConstants.NoteCategoryDoesNotExists); } if (noteCategory.Name == "Task") { note.IsActiveTask = true; } } await this.context.Notes.AddAsync(note); await this.context.SaveChangesAsync(); var result = await this.context.Notes .Include(x => x.User) .Include(x => x.NoteCategory) .FirstOrDefaultAsync(x => x.Id == note.Id); return(result.ToDTO()); }