public async Task <IActionResult> Post([FromBody] CreateNoteDto createNoteDto) { var noteId = Guid.NewGuid(); await _noteService.CreateAsync(noteId, createNoteDto.Title, createNoteDto.Content); return(Created($"Notes/{noteId}", null)); }
public async Task <ServiceResponse <NoteDisplayDto> > InsertNote(CreateNoteDto noteDto) { var userId = _userAccessor.GetUserIdFromContext(); var serviceResponse = new ServiceResponse <NoteDisplayDto>(); var newNote = new Note { Title = noteDto.Title, Content = noteDto.Content, Keywords = noteDto.Keywords, User = await _context.Users.SingleOrDefaultAsync(u => u.Id.ToString() == userId) }; await _context.Notes.AddAsync(newNote); var result = await _context.SaveChangesAsync() > 0; if (!result) { serviceResponse.Success = false; serviceResponse.Message = "Can not save note to database"; return(serviceResponse); } serviceResponse.Data = _mapper.Map <NoteDisplayDto>(newNote); return(serviceResponse); }
public async Task Given_valid_data_note_should_be_created() { var newNote = new CreateNoteDto { Title = "New title", Content = "New content" }; var serializedNote = SerializeData(newNote); var response = await Client.PostAsync("Notes", serializedNote); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.Created); var newNoteLoc = response.Headers.Location.ToString(); response = await Client.GetAsync(newNoteLoc); var responseString = await response.Content.ReadAsStringAsync(); var createdNote = JsonConvert.DeserializeObject <NoteDetailsDto>(responseString); createdNote.LastVersion.Title .Should() .BeEquivalentTo(newNote.Title); createdNote.LastVersion.Content .Should() .BeEquivalentTo(newNote.Content); createdNote.LastVersion.VersionNo .Should() .Be(1); }
public async Task Given_invalid_content_update_should_fail() { var note = new CreateNoteDto { Title = "New title", Content = "New content" }; var serializedNote = SerializeData(note); var response = await Client.PostAsync("Notes", serializedNote); var newNoteLoc = response.Headers.Location.ToString(); var noteWithUpdatedTitle = new UpdateNoteDto { Title = "", Content = "Updated content" }; serializedNote = SerializeData(noteWithUpdatedTitle); response = await Client.PutAsync(newNoteLoc, serializedNote); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.BadRequest); }
public async Task Given_valid_noteId_note_should_be_deleted() { var newNote = new CreateNoteDto { Title = "New title", Content = "New content" }; var serializedNote = SerializeData(newNote); var response = await Client.PostAsync("Notes", serializedNote); var newNoteLoc = response.Headers.Location.ToString(); //Deleting note response = await Client.DeleteAsync(newNoteLoc); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.NoContent); //Searching for deleted note should fail response = await Client.GetAsync(newNoteLoc); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.NotFound); }
public async Task Given_invalid_noteId_update_should_fail() { var note = new CreateNoteDto { Title = "New title", Content = "New content" }; var serializedNote = SerializeData(note); var response = await Client.PostAsync("Notes", serializedNote); var newNoteLoc = response.Headers.Location.ToString(); var noteWithUpdatedTitle = new UpdateNoteDto { Title = "New title", Content = "Updated content" }; serializedNote = SerializeData(noteWithUpdatedTitle); var invalidUrl = $"Notes/{Guid.NewGuid()}"; response = await Client.PutAsync(invalidUrl, serializedNote); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.NotFound); }
public async Task <ServiceResponse <List <GetNoteDto> > > CreateNote(CreateNoteDto newNote) { // Response ServiceResponse <List <GetNoteDto> > serviceResponse = new ServiceResponse <List <GetNoteDto> >(); // Map to Note Note note = _mapper.Map <Note>(newNote); // Set the note's user based on Id note.User = await _context.Users.FirstOrDefaultAsync(u => u.Id == GetUserId()); note.BreedingRecordId = newNote.BreedingRecordId; note.ContactId = newNote.ContactId; note.Title = newNote.Title; note.Body = newNote.Body; // Save await _context.Notes.AddAsync(note); await _context.SaveChangesAsync(); // Return all the notes serviceResponse.Data = await GetAllRecords(); return(serviceResponse); }
public async Task <IActionResult> CreateNote(int userId, [FromBody] CreateNoteDto note) { try { Guard.Against.NegativeOrZero(userId, nameof(userId)); var validationResult = await ValidateCreateNote(note); if (validationResult.ValidationErrors.Any()) { return(BadRequest(validationResult)); } return(Ok(await _noteService.CreateNote(userId, note))); } catch (ArgumentException ex) { return(GetErrorResponse(ex, StatusCodes.Status400BadRequest)); } catch (NotFoundException ex) { return(GetErrorResponse(ex, StatusCodes.Status404NotFound)); } catch (Exception ex) { return(GetErrorResponse(ex, StatusCodes.Status500InternalServerError, "An error occurred when attempting to create a new note.")); } }
public async Task <IActionResult> Create( [FromServices] ICreateNoteUseCase useCase, [FromServices] CreateNotePresenter presenter, [FromBody] CreateNoteDto _) { await useCase.Execute(new CreateNoteInput(_.Title, _.Text)); return(presenter.ViewModel); }
//[ProducesResponseType(typeof(User), 201)] //[ProducesResponseType(typeof(ErrorResource), 400)] public async Task <IActionResult> PostAsync([FromBody] CreateNoteDto resource) { var note = await _mediator.Send(new CreateNoteCommand() { Note = resource }); return(Created($"/api/notes/{note.NoteId}", note)); }
public async Task <int> CreateNote(int userId, CreateNoteDto note) { var user = await _userProvider.GetUser(userId); if (user == null) { throw new NotFoundException(); } return(await _noteProvider.CreateNote(userId, note)); }
private async Task <ValidationErrorResponse> ValidateCreateNote(CreateNoteDto note) { ValidationErrorResponse validationErrorResponse = new ValidationErrorResponse(null, null, new List <ValidationError>()); var validation = _createNoteValidator.Validate(note); if (validation?.IsValid == false) { AddValidationErrors(validationErrorResponse, validation.Errors); } return(validationErrorResponse); }
public async Task Given_invalid_title_create_should_fail() { var note = new CreateNoteDto { Title = "", Content = "New content" }; var serializedNote = SerializeData(note); var response = await Client.PostAsync("Notes", serializedNote); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.BadRequest); }
public int CreateNote(int categoryId, CreateNoteDto newNote) { var category = GetCategoryById(categoryId); var noteDb = _mapper.Map <Note>(newNote); noteDb.CategoryId = category.Id; _appDbContext.Notes.Add(noteDb); _appDbContext.SaveChanges(); _logger.LogTrace($"Category with id: {categoryId} created new note"); return(noteDb.Id); }
public async Task <ActionResult <Note> > CreateNote([FromBody] CreateNoteDto createNote) { Note note = new Note { Body = createNote.Body, Title = createNote.Title, CreatedAt = DateTime.Now, ModifiedAt = DateTime.Now }; _context.Notes.Add(note); await _context.SaveChangesAsync(); return(note); }
public ActionResult CreateMI(CreateNoteDto targetValue) { var userIdString = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var userId = int.Parse(userIdString); var data = dataContext.Set <Note>(); data.Add(new Note { UserId = userId, NoteContent = targetValue.Content, Type = NoteType.MainIdea, CreationDate = DateTimeOffset.Now, LectureId = targetValue.LectureId }); return(Ok()); }
public async Task Given_valid_noteId_note_should_exists() { var newNote = new CreateNoteDto { Title = "New title", Content = "New content" }; var serializedNote = SerializeData(newNote); var response = await Client.PostAsync("Notes", serializedNote); var newNoteLoc = response.Headers.Location.ToString(); response = await Client.GetAsync(newNoteLoc); response.StatusCode .Should() .BeEquivalentTo(HttpStatusCode.OK); }
public int Create(CreateNoteDto dto, int templateId) { var template = _dbContext .Templates .Include(t => t.Categories.OrderBy(c => c.Order)) .ThenInclude(c => c.Labels.OrderBy(l => l.Order)) .Where(t => t.UserId == _contextService.GetUserId) .Where(t => t.Id == templateId) .SingleOrDefault(); if (template is null) { throw new BadRequestException("Template not found"); } var inputs = _mapper.Map <List <Input> >(dto.Inputs); CountInputs(inputs); var note = new Note { Name = dto.Name, Template = template, Inputs = inputs, UserId = _contextService.GetUserId }; var labelCounter = 0; foreach (var category in note.Template.Categories) { labelCounter += category.Labels.Count; } if (note.Inputs.Count != labelCounter) { throw new BadRequestException("Number of inputs does not match number of labels"); } _dbContext.Add(note); _dbContext.SaveChanges(); return(note.Id); }
public async Task<int> CreateNote(int userId, CreateNoteDto note) { var query = "INSERT INTO \"Note\" (\"UserId\", \"Title\", \"Content\", \"CreatedOnUTC\") " + "VALUES (@UserId, @Title, @Content, @CreatedOn) " + "RETURNING \"NoteId\""; var queryParams = new { UserId = userId, Title = note.Title, Content = note.Content, CreatedOn = DateTime.UtcNow }; using (var connection = new NpgsqlConnection(_connectionString)) { return await connection.QueryFirstOrDefaultAsync<int>(query, queryParams); } }
public async Task <ActionResult <Note> > UpdateNote(int id, [FromBody] CreateNoteDto createNote) { Note note = await _context.Notes.FindAsync(id); if (note == null) { return(new NotFoundResult()); } note.Body = createNote.Body; note.Title = createNote.Title; note.ModifiedAt = DateTime.Now; _context.Notes.Update(note); await _context.SaveChangesAsync(); return(note); }
public async Task <ActionResult <NoteDto> > CreateNote(CreateNoteDto createNoteDto) { var user = await _userRepository.GetUserByIdAsync(User.GetUserId()); var note = new Note { AuthorId = User.GetUserId(), Author = user, JobId = createNoteDto.JobId, Content = createNoteDto.Content, Created = DateTime.Now }; _noteRepository.AddNote(note); if (await _noteRepository.SaveAllAsync()) { return(Ok(_mapper.Map <NoteDto>(note))); } return(BadRequest("Something unexpected happened.")); }
public ActionResult <Note> Post([FromBody] CreateNoteDto note) { if (note == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest()); } var finalNote = Mapper.Map <Note>(note); _noteRepository.AddNote(finalNote); if (!_noteRepository.Save()) { return(StatusCode(500, "A problem happened while saving your request")); } return(CreatedAtRoute("GetNote", new { id = finalNote.Id }, finalNote)); }
public ActionResult Post([FromRoute] int categoryId, [FromBody] CreateNoteDto newNote) // nazwa parametru categoryId musi sie zgadzac ze sciezka z Route kontrolera { var newId = _noteService.CreateNote(categoryId, newNote); return(Created($"/category/{categoryId}/note/{newId}", null)); }
public async Task <IActionResult> CreateNote(CreateNoteDto newNote) { return(Ok(await _NoteService.CreateNote(newNote))); }
public ActionResult Create([FromBody] CreateNoteDto dto, [FromRoute] int templateId) { int id = _service.Create(dto, templateId); return(Created($"note/{id}", null)); }
public async Task <IActionResult> Insert([FromBody] CreateNoteDto noteDto) { return(Created(new Uri(Request.Path, UriKind.Relative), await _noteService.InsertNote(noteDto))); }
public async Task <ActionResult <NoteDto> > CreateNote([FromBody] CreateNoteDto createNoteModel, CancellationToken cancellationToken) { var response = await _mediator.Send(new CreateNoteCommand(createNoteModel), cancellationToken); return(ProcessResult(response)); }