Skip to content

Commit

Permalink
FULL-STACK-CHANGE: This change will fail on deploy as contracts are n…
Browse files Browse the repository at this point in the history
…ot changed safely. Run npm `run test:aws:loop` before running deployment to see when it fails.
  • Loading branch information
chrissimon-au committed May 22, 2023
1 parent 92f156c commit a1a8d1a
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 64 deletions.
34 changes: 22 additions & 12 deletions app/src/app/names.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@
import { useState, useMemo, ChangeEvent, FormEvent } from 'react';
import dynamic from 'next/dynamic';

type FullName = {firstName: string, lastName: string};
const DefaultFullName: FullName | null = null;
const InitialFullName: FullName = { firstName: '', lastName: '' };

function Names() {
const [inputName, setInputName] = useState('');
const [inputName, setInputName] = useState(DefaultFullName);
const [loading, setLoading] = useState(false);
const [name, setName] = useState('');
const [name, setName] = useState(DefaultFullName);

const config = useMemo(() => fetch('config.json')
.then(response => response.json()), []);

const onChange = (e:ChangeEvent<HTMLInputElement>) => {
setInputName(e.target.value);
setInputName({ ...(inputName || InitialFullName), [e.target.id]: e.target.value});
}

const setNameResult = (name: string) => {
const setNameResult = (name: FullName | null) => {
setLoading(!!!name);
setName(name);
setName(name || DefaultFullName);
}

const addPerson = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setNameResult('');
setNameResult(null);

config
.then(c =>
fetch(`${c.SERVER_URL}/names`, {
method: "POST",
body: JSON.stringify({
name: inputName
fullName: inputName
}),
headers: {
'Content-type': 'application/json',
Expand All @@ -37,17 +41,23 @@ function Names() {
.then(response => fetch(response.headers.get("Location")!))
.then(response => response.json())
.then(n => {
setNameResult(n.name);
setNameResult(n.fullName);
});
};

return (
<form className="form" onSubmit={addPerson}>
<div className="mb-4">
<label className="label" htmlFor="name">
Name
<label className="label" htmlFor="firstName">
Firstname
</label>
<input className="input" id="firstName" value={inputName && inputName.firstName || ''} onChange={onChange} type="text" placeholder="Name"/>
</div>
<div className="mb-4">
<label className="label" htmlFor="lastName">
Lastname
</label>
<input className="input" id="name" value={inputName} onChange={onChange} type="text" placeholder="Name"/>
<input className="input" id="lastName" value={inputName && inputName.lastName || ''} onChange={onChange} type="text" placeholder="Name"/>
</div>
<div>
<button className="btn" type="submit">
Expand All @@ -56,7 +66,7 @@ function Names() {
</div>
{loading && <div className="result">loading...</div>}
{name && <div className="result">
{name}
{name.firstName} {name.lastName}
</div>}
</form>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package au.chrissimon.safecontractchangesdemo;

public class AddNameRequest {
private String name;
private FullNameDto fullName;

public String getName() {
return name;
public FullNameDto getFullName() {
return fullName;
}

public void setName(String name) {
this.name = name;
public void setFullName(FullNameDto fullName) {
this.fullName = fullName;
}

public AddNameRequest() {
super();
}

public AddNameRequest(String name) {
public AddNameRequest(FullNameDto fullName) {
super();
this.name = name;
this.fullName = fullName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package au.chrissimon.safecontractchangesdemo;

import jakarta.persistence.Embeddable;

@Embeddable
public class FullName {
private String firstName;
public String getFirstName() {
return firstName;
}
private String lastName;
public String getLastName() {
return lastName;
}
public FullName() {
super();
}
public FullName(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}

public FullNameDto toResponse() {
return new FullNameDto(getFirstName(), getLastName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package au.chrissimon.safecontractchangesdemo;

public class FullNameDto {
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public FullNameDto() {
super();
}
public FullNameDto(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@

import java.util.UUID;

import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Name {
private @Id UUID id;
private String name;
@Embedded private FullName name;

public UUID getId() {
return id;
}

public String getName() {
public FullName getName() {
return name;
}

public Name() {
super();
}

public Name(String name) {
public Name(FullName name) {
super();
this.id = UUID.randomUUID();
this.name = name;
}

public NameResponse toResponse() {
return new NameResponse(id, name);
return new NameResponse(id, name.toResponse());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.UUID;

public class NameResponse {
private String name;
private FullNameDto fullName;
private UUID id;

public UUID getId() {
Expand All @@ -14,21 +14,21 @@ public void setId(UUID id) {
this.id = id;
}

public String getName() {
return name;
public FullNameDto getFullName() {
return fullName;
}

public void setName(String name) {
this.name = name;
public void setFullName(FullNameDto fullName) {
this.fullName = fullName;
}

public NameResponse() {
super();
}

public NameResponse(UUID id, String name) {
public NameResponse(UUID id, FullNameDto fullName) {
super();
this.id = id;
this.name = name;
this.fullName = fullName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public NamesController(NameRepository nameRepository) {
@PostMapping("/names")
public ResponseEntity<NameResponse> addName(@RequestBody AddNameRequest request) {

Name name = new Name(request.getName());
FullNameDto fullName = request.getFullName();
Name name = new Name(new FullName(fullName.getFirstName(), fullName.getLastName()));

nameRepository.save(name);

Expand Down
25 changes: 25 additions & 0 deletions server/src/main/resources/db/changelog/02_add_full_name.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
databaseChangeLog:
- changeSet:
id: 2
author: Chris Simon
changes:
- addColumn:
tableName: name
columns:
- column:
name: first_name
type: varchar(255)
constraints:
nullable: true
unique: false
- column:
name: last_name
type: varchar(255)
constraints:
nullable: true
unique: false
- dropColumn:
tableName: name
columns:
- column:
name: name
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
databaseChangeLog:
- include:
file: db/changelog/01_initial_schema.yml
file: db/changelog/01_initial_schema.yml
- include:
file: db/changelog/02_add_full_name.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public void AddPerson() {
.baseUrl("http://localhost:" + port)
.build();

String name = UUID.randomUUID().toString();
String firstName = UUID.randomUUID().toString();
String lastName = UUID.randomUUID().toString();

TestAddNameRequest nameRequest = new TestAddNameRequest(name);
TestAddNameRequest nameRequest = new TestAddNameRequest(new TestFullName(firstName, lastName));

ResponseSpec response = client
.post()
Expand All @@ -47,7 +48,9 @@ public void AddPerson() {
.getResponseHeaders().getLocation();

response.expectStatus().isCreated();
assertThat(nameResponse.getName()).isEqualTo(name);
TestFullName fullName = nameResponse.getFullName();
assertThat(fullName.getFirstName()).isEqualTo(firstName);
assertThat(fullName.getLastName()).isEqualTo(lastName);
assertThat(nameResponse.getId()).isNotNull();
assertThat(nameResponse.getId()).isNotEqualTo(new UUID(0, 0));
assertThat(newNameLocation.toString()).isEqualTo("http://localhost:" + port + "/names/" + nameResponse.getId());
Expand All @@ -65,6 +68,8 @@ public void AddPerson() {
.getResponseBody();

assertThat(checkNameResponse.getId()).isEqualTo(nameResponse.getId());
assertThat(checkNameResponse.getName()).isEqualTo(name);
TestFullName checkFullName = nameResponse.getFullName();
assertThat(checkFullName.getFirstName()).isEqualTo(firstName);
assertThat(checkFullName.getLastName()).isEqualTo(lastName);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package au.chrissimon.safecontractchangesdemo;

public class TestAddNameRequest {
private String name;
private TestFullName fullName;

public String getName() {
return name;
public TestFullName getFullName() {
return fullName;
}

public void setName(String name) {
this.name = name;
public void setFullName(TestFullName fullName) {
this.fullName = fullName;
}

public TestAddNameRequest() {
super();
}

public TestAddNameRequest(String name) {
public TestAddNameRequest(TestFullName fullName) {
super();
this.name = name;
this.fullName = fullName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package au.chrissimon.safecontractchangesdemo;

public class TestFullName {
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public TestFullName() {
super();
}
public TestFullName(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
Loading

0 comments on commit a1a8d1a

Please sign in to comment.