Skip to content

Commit

Permalink
feat: add patch request to library service
Browse files Browse the repository at this point in the history
  • Loading branch information
thisdudkin committed Sep 12, 2024
1 parent 43a0ed6 commit c910878
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
@JsonInclude(NON_NULL)
public record BookRecordDto(
Integer bookId,
BookStatus bookStatus
BookStatus status
) { }
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import dev.earlspilner.library.dto.BookRecordDto;
import dev.earlspilner.library.model.BookRecord;
import dev.earlspilner.library.model.BookStatus;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
* @author Alexander Dudkin
*/
@Mapper(componentModel = "spring")
public interface BookRecordMapper {
BookRecord toEntity(BookRecordDto dto);

@Mapping(source = "status", target = "status")
BookRecordDto toDto(BookRecord entity);

@Mapping(source = "status", target = "status")
BookRecord toEntity(BookRecordDto dto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public interface LibraryApi {
ResponseEntity<BookRecordDto> addBootRecord(BookRecordDto dto);
ResponseEntity<BookRecordDto> getBookRecord(Integer bookId);
ResponseEntity<BookRecordDto> updateBookRecord(Integer bookId, BookRecordDto dto);
ResponseEntity<Void> deleteBookRecord(Integer bookId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,4 @@ public ResponseEntity<BookRecordDto> updateBookRecord(@PathVariable Integer book
return new ResponseEntity<>(libraryService.updateBookRecord(bookId, dto), HttpStatus.OK);
}

@Override
@DeleteMapping("/{bookId}")
public ResponseEntity<Void> deleteBookRecord(@PathVariable Integer bookId) {
libraryService.deleteBookRecord(bookId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public interface LibraryService {
BookRecordDto addBookRecord(BookRecordDto dto);
BookRecordDto getBookRecord(Integer id);
BookRecordDto updateBookRecord(Integer bookId, BookRecordDto dto);
void deleteBookRecord(Integer id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.earlspilner.library.mapper.BookRecordMapper;
import dev.earlspilner.library.model.BookRecord;
import dev.earlspilner.library.repository.BookRecordRepository;
import dev.earlspilner.library.rest.advice.BookRecordNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -30,16 +31,22 @@ public BookRecordDto addBookRecord(BookRecordDto dto) {

@Override
public BookRecordDto getBookRecord(Integer id) {
return null;
BookRecord bookRecord = bookRecordRepository.findById(id)
.orElseThrow(() -> new BookRecordNotFoundException("Book not found with ID: " + id));
return bookRecordMapper.toDto(bookRecord);
}

@Override
public BookRecordDto updateBookRecord(Integer bookId, BookRecordDto dto) {
return null;
}
if (dto.status() == null) {
throw new IllegalArgumentException("Book record status is not configured for this operation");
}

@Override
public void deleteBookRecord(Integer id) {
bookRecordRepository.deleteById(id);
BookRecord bookRecord = bookRecordRepository.findById(bookId)
.orElseThrow(() -> new BookRecordNotFoundException("Book not found with ID: " + bookId));

bookRecord.setStatus(dto.status());
return bookRecordMapper.toDto(bookRecordRepository.save(bookRecord));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.earlspilner.users.service;

import java.math.BigInteger;
import java.security.SecureRandom;

/**
* @author Alexander Dudkin
*/
public class KeyGen {

private static final SecureRandom secureRandom = new SecureRandom();
private static final int BIT_LENGTH = 256;
private static final int RADIX = 16;

public static String generateKey() {
BigInteger key = new BigInteger(BIT_LENGTH, secureRandom);
return key.toString(RADIX);
}

public static void main(String[] args) {
System.out.println(generateKey());
}

}

0 comments on commit c910878

Please sign in to comment.