Skip to content

Commit

Permalink
Merge pull request #29 from Staketab/dev
Browse files Browse the repository at this point in the history
add edit domain endpoint, and add exception handler
  • Loading branch information
MrFoxogen authored Mar 19, 2024
2 parents 78dc804 + 5446673 commit dfb718e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.NoSuchElementException;

import static org.springframework.http.ResponseEntity.notFound;
import static org.springframework.http.ResponseEntity.ok;

@RestController
Expand Down Expand Up @@ -75,20 +72,12 @@ public ResponseEntity<DomainEntity> create(@RequestBody DomainReservationDTO dom

@GetMapping("/{id}")
public ResponseEntity<DomainEntity> retrieve(@PathVariable String id) {
try {
return ok(domainService.retrieve(id).orElseThrow(NoSuchElementException::new));
} catch (NoSuchElementException e) {
return notFound().build();
}
return ok(domainService.retrieve(id));
}

@PutMapping("/edit")
public ResponseEntity<DomainEntity> update(@RequestBody DomainUpdateDTO domainUpdate) {
try {
return ok(domainService.update(domainUpdate));
} catch (NoSuchElementException e) {
return notFound().build();
}
return ok(domainService.update(domainUpdate));
}

@GetMapping("/{domainName}/reserved")
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/staketab/minanames/dto/DomainUpdateDTO.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.staketab.minanames.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DomainUpdateDTO {

private String id;
private String img;
}
9 changes: 9 additions & 0 deletions src/main/java/com/staketab/minanames/exception/ApiError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.staketab.minanames.exception;

import org.springframework.http.HttpStatusCode;

public record ApiError(String statusCode, String message) {
public ApiError(HttpStatusCode statusCode, String message) {
this(statusCode.toString(), message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.staketab.minanames.exception;

public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.staketab.minanames.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static org.springframework.http.HttpStatus.NOT_FOUND;

@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {

@ResponseStatus(NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ApiError> handleException(Exception e) {
return ResponseEntity.status(NOT_FOUND).body(new ApiError(NOT_FOUND, e.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import com.staketab.minanames.entity.DomainEntity;
import org.springframework.data.domain.Page;

import java.util.Optional;

public interface DomainService {
Page<DomainEntity> findAllByPageable(BaseRequest request, SearchParams searchParams);
Page<DomainEntity> findAllByAccountPageable(BaseRequest request, String accountAddress, SearchParams searchParams);
DomainEntity create(DomainReservationDTO domainRequest);
Optional<DomainEntity> retrieve(String id);
DomainEntity retrieve(String id);
DomainEntity update(DomainUpdateDTO domainUpdateDTO);
ReservedDomainDTO isNameReserved(String name);
Boolean setDefaultDomain(String id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.dto.DomainStatus;
import com.staketab.minanames.exception.NotFoundException;
import com.staketab.minanames.repository.DomainRepository;
import com.staketab.minanames.service.abstraction.DomainService;
import com.staketab.minanames.service.abstraction.TxService;
Expand Down Expand Up @@ -55,13 +56,20 @@ public DomainEntity create(DomainReservationDTO request) {
}

@Override
public Optional<DomainEntity> retrieve(String id) {
return domainRepository.findById(id);
public DomainEntity retrieve(String id) {
return domainRepository.findById(id)
.orElseThrow(() -> new NotFoundException(String.format("Domain doesn't found by id: %s", id)));
}

@Override
public DomainEntity update(DomainUpdateDTO domainUpdateDTO) {
return null;
Optional<DomainEntity> optionalDomainEntity = domainRepository.findById(domainUpdateDTO.getId());
if (optionalDomainEntity.isEmpty()) {
throw new NotFoundException(String.format("Domain doesn't found by id: %s", domainUpdateDTO.getId()));
}
DomainEntity domainEntity = optionalDomainEntity.get();
domainEntity.setDomainImg(domainUpdateDTO.getImg());
return domainRepository.save(domainEntity);
}

@Override
Expand Down

0 comments on commit dfb718e

Please sign in to comment.