Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers("/api/v1/form/me/**").authenticated()
.requestMatchers("/api/v1/user/me").authenticated()
.requestMatchers("/api/v1/user/me/**").authenticated()
.requestMatchers("**").permitAll());
.requestMatchers("/**").permitAll());

http.exceptionHandling(handling -> handling.accessDeniedPage("/access-denied"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import br.com.arnar.openforms.api.OpenFormsApplication;
import br.com.arnar.openforms.api.database.Form;
import br.com.arnar.openforms.api.database.User;
import br.com.arnar.openforms.api.exception.NoSuchEntryException;
import br.com.arnar.openforms.api.request.form.FormSendRequest;
import br.com.arnar.openforms.api.response.SimpleFormsResponse;
import br.com.arnar.openforms.api.response.SimplifiedForm;
import br.com.arnar.openforms.api.service.FormServiceInterface;
import br.com.arnar.openforms.api.service.UserServiceInterface;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -60,4 +62,39 @@ public ResponseEntity<?> listAllForMe(HttpServletRequest request) {

return ok(new SimpleFormsResponse(forms));
}

@Transactional
@GetMapping(path = "/me/{id}", produces = "application/json")
public ResponseEntity<?> getByIdForMe(@PathVariable Long id, HttpServletRequest request) {
User me = userService.getMe(request);

Form forms = service.getById(id);

if (!forms.getOwner().getId().equals(me.getId())) {
throw new NoSuchEntryException("Unable to find a form with this id");
}

return ok(new SimplifiedForm(forms));
}

@Transactional
@GetMapping(path = "/me/{id}/view")
public ResponseEntity<?> setFormVisualized(@PathVariable Long id, HttpServletRequest request) {
User me = userService.getMe(request);

Form form = service.getById(id);

if (!form.getOwner().getId().equals(me.getId())) {
throw new NoSuchEntryException("Unable to find a form with this id");
}

if (form.getVisualized()) {
return ok(new SimplifiedForm(form));
}

form.setVisualized(true);
service.insert(form);

return ok(new SimplifiedForm(form));
}
}
5 changes: 5 additions & 0 deletions src/main/java/br/com/arnar/openforms/api/database/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

Expand Down Expand Up @@ -47,4 +48,8 @@ public class Form {

@Column(name = "message")
private String message;

@ColumnDefault("false")
@Column(name = "visualized")
private Boolean visualized;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@

@Getter
@Setter
class SimplifiedForm {
public class SimplifiedForm {
public Long id;
public String name;
public String phoneNumber;
public String email;
public String message;
public Boolean visualized;

public SimplifiedForm(Form form) {
setId(form.getId());
setName(form.getName());
setPhoneNumber(form.getPhoneNumber());
setEmail(form.getEmail());
setMessage(form.getMessage());
setVisualized(form.getVisualized());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public interface FormServiceInterface {
List<Form> getByOwner(User owner);
Form getById(Long id);
Form insert(Form entity, Long ownerId);
Form insert(Form entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public Form insert(Form entity, Long ownerId) {

return repository.save(entity);
}

@Override
public Form insert(Form entity) {
return repository.save(entity);
}
}
5 changes: 4 additions & 1 deletion src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ INSERT INTO member (username, email, password) VALUES
('mock.admin', 'mock.admin@gmail.com', '061cf224cffe1951e32ffaa1c414544a9dae01c216e0cd89aa5d496cb45a968202d755be85db9c1f8a565a7b4846c2cba027b81cdfb159b023b2ee3a94da7de9');

INSERT INTO form (owner_id, name, phone_number, email, message) VALUES
(3, 'Mock User Client', '5521921232132', 'mock.disposable@gmail.com', 'hello there');
(3, 'Mock User Client', '5521921232132', 'mock.disposable@gmail.com', 'hello there');

INSERT INTO form (owner_id, name, phone_number, email, message) VALUES
(2, 'Conta 2 Client', '5521921232132', 'mock.disposable@gmail.com', 'hello there');
1 change: 1 addition & 0 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CREATE TABLE form (
phone_number varchar(128),
email varchar(128),
message varchar(1280),
visualized boolean DEFAULT false,
PRIMARY KEY (id),
CONSTRAINT fk_owner_id FOREIGN KEY(owner_id) REFERENCES member(id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,39 @@ void getAllForMeNotAuthenticated() throws Exception {
req.get("/form/me/listAll").andExpect(status().isForbidden());
}

@Test
void getByIdForMe() throws Exception {
String jwt = MockValues.getUserJwt(mockMvc);
req.get("/form/me/1", jwt).andExpect(status().isOk());
}

@Test
void getByIdExistentButNotMine() throws Exception {
String jwt = MockValues.getUserJwt(mockMvc);
req.get("/form/me/2", jwt).andExpect(status().isNotFound());
}

@Test
void getByIdInexistent() throws Exception {
String jwt = MockValues.getUserJwt(mockMvc);
req.get("/form/me/23", jwt).andExpect(status().isNotFound());
}

@Test
void visualize() throws Exception {
String jwt = MockValues.getUserJwt(mockMvc);
req.get("/form/me/1/view", jwt).andExpect(status().isOk());
}

@Test
void visualizeExistentBotNotMine() throws Exception {
String jwt = MockValues.getUserJwt(mockMvc);
req.get("/form/me/2/view", jwt).andExpect(status().isNotFound());
}

@Test
void visualizeInexistent() throws Exception {
String jwt = MockValues.getUserJwt(mockMvc);
req.get("/form/me/2231/view", jwt).andExpect(status().isNotFound());
}
}