Skip to content

Commit

Permalink
Add ability to add or update an Owner
Browse files Browse the repository at this point in the history
* add create form handling
* add update form handing
closes #55
  • Loading branch information
arrumm committed Aug 11, 2020
1 parent 3455018 commit e60d404
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import static java.util.Objects.isNull;

@Setter
@Getter
@NoArgsConstructor
Expand All @@ -22,4 +24,9 @@ public class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public Boolean isNew() {
return isNull(this.id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

Expand All @@ -21,6 +24,9 @@
@Controller
public class OwnerController {

public static final String REDIRECT_OWNERS = "redirect:/owners/";
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";

private final OwnerService ownerService;

public OwnerController(final OwnerService ownerService) {
Expand All @@ -44,7 +50,7 @@ public String processFindForm(Owner owner, BindingResult result, Model model) {
return "owners/findOwners";
} else if (results.size() == 1) {
owner = results.get(0);
return "redirect:/owners/" + owner.getId();
return REDIRECT_OWNERS + owner.getId();
} else {
model.addAttribute("selections", results);
return "owners/ownersList";
Expand All @@ -64,4 +70,37 @@ public ModelAndView showOwner(@PathVariable("ownerId") Long ownerId) {
return modelAndView;
}

@GetMapping("/new")
public String initCreationForm(Model model) {
model.addAttribute("owner", Owner.builder().build());
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}

@PostMapping("/new")
public String processCreationForm(@Valid Owner owner, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else {
final Owner savedOwner = ownerService.save(owner);
return REDIRECT_OWNERS + savedOwner.getId();
}
}

@GetMapping("/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable Long ownerId, Model model) {
model.addAttribute(ownerService.findById(ownerId));
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}

@PostMapping("/{ownerId}/edit")
public String processUpdateOwnerForm(@Valid Owner owner, @PathVariable Long ownerId, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else {
owner.setId(ownerId);
final Owner savedOwner = ownerService.save(owner);
return REDIRECT_OWNERS + savedOwner.getId();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
Expand Down Expand Up @@ -99,7 +98,6 @@ void processFindFormReturnOne() throws Exception {
//@formatter:on
}

@Disabled
@Test
void initCreationForm() throws Exception {
//@formatter:off
Expand All @@ -111,7 +109,6 @@ void initCreationForm() throws Exception {
verifyNoMoreInteractions(ownerService);
}

@Disabled
@Test
void processCreationForm() throws Exception {
when(ownerService.save(ArgumentMatchers.any())).thenReturn(Owner.builder().id(1L).build());
Expand All @@ -124,7 +121,6 @@ void processCreationForm() throws Exception {
verify(ownerService).save(ArgumentMatchers.any());
}

@Disabled
@Test
void initUpdateOwnerForm() throws Exception {
when(ownerService.findById(anyLong())).thenReturn(Owner.builder().id(1L).build());
Expand All @@ -137,7 +133,6 @@ void initUpdateOwnerForm() throws Exception {
verifyNoMoreInteractions(ownerService);
}

@Disabled
@Test
void processUpdateOwnerForm() throws Exception {
when(ownerService.save(ArgumentMatchers.any())).thenReturn(Owner.builder().id(1L).build());
Expand Down

0 comments on commit e60d404

Please sign in to comment.