Completing the CRUD for the widget domain.
This commit is contained in:
parent
e4f59ff941
commit
9cea9f94f2
|
|
@ -29,19 +29,19 @@ public class WidgetController extends BaseController {
|
|||
@GetMapping("")
|
||||
public Flux<ResponseDto<Widget>> getAllWidgets() {
|
||||
return widgetService.get()
|
||||
.map(user -> new ResponseDto<>(HttpStatus.OK.value(), user, null));
|
||||
.map(widgets -> new ResponseDto<>(HttpStatus.OK.value(), widgets, null));
|
||||
}
|
||||
|
||||
@GetMapping("/{name}")
|
||||
public Mono<ResponseDto<Widget>> getByName(@PathVariable String id) {
|
||||
return widgetService.getByName(id)
|
||||
.map(user -> new ResponseDto<>(HttpStatus.OK.value(), user, null));
|
||||
public Mono<ResponseDto<Widget>> getByName(@PathVariable String name) {
|
||||
return widgetService.getByName(name)
|
||||
.map(widget -> new ResponseDto<>(HttpStatus.OK.value(), widget, null));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public Mono<ResponseDto<Widget>> getCureFitUser(@PathVariable Long id) {
|
||||
return widgetService.update(id)
|
||||
.map(cfUser -> new ResponseDto<>(HttpStatus.OK.value(), cfUser, null));
|
||||
@PutMapping("")
|
||||
public Mono<ResponseDto<Widget>> update(@RequestBody Widget widget) throws Exception {
|
||||
return widgetService.update(widget)
|
||||
.map(updatedWidget -> new ResponseDto<>(HttpStatus.OK.value(), updatedWidget, null));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.mobtools.server.domains;
|
|||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.DynamicUpdate;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedBy;
|
||||
|
|
@ -18,6 +19,7 @@ import java.util.Date;
|
|||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@DynamicUpdate
|
||||
public abstract class BaseDomain implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7459916000501322517L;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import lombok.Setter;
|
|||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import lombok.Setter;
|
|||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Entity
|
||||
// Specially adding the table name here because the keyword "User" is reserved in Postgres
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import lombok.Setter;
|
|||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
|
|
|
|||
|
|
@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository;
|
|||
@Repository
|
||||
public interface WidgetRepository extends BaseRepository<Widget, Long> {
|
||||
|
||||
Widget findByName(String name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import reactor.core.publisher.Mono;
|
|||
|
||||
public interface WidgetService {
|
||||
|
||||
Mono<Widget> getByName(String id);
|
||||
Mono<Widget> getByName(String name);
|
||||
|
||||
Flux<Widget> get();
|
||||
|
||||
Mono<Widget> create(Widget widget);
|
||||
|
||||
Mono<Widget> update(Long id);
|
||||
Mono<Widget> update(Widget widget) throws Exception;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,13 +22,16 @@ public class WidgetServiceImpl extends BaseService implements WidgetService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Mono<Widget> getByName(String id) {
|
||||
return null;
|
||||
public Mono<Widget> getByName(String name) {
|
||||
return Mono.fromCallable(() -> widgetRepository.findByName(name))
|
||||
.subscribeOn(scheduler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<Widget> get() {
|
||||
return null;
|
||||
return Mono.fromCallable(() -> widgetRepository.findAll())
|
||||
.flatMapMany(Flux::fromIterable)
|
||||
.subscribeOn(scheduler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -39,7 +42,13 @@ public class WidgetServiceImpl extends BaseService implements WidgetService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Mono<Widget> update(Long id) {
|
||||
return null;
|
||||
public Mono<Widget> update(Widget widget) throws Exception {
|
||||
if(widget.getId() == null) {
|
||||
throw new Exception("Invalid id provided");
|
||||
}
|
||||
|
||||
return Mono.fromCallable(
|
||||
() -> widgetRepository.save(widget)
|
||||
).subscribeOn(this.scheduler);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user