' + row.id + ' | ' +
21 | '' + row.multiplication.factorA + ' x ' + row.multiplication.factorB + ' | ' +
22 | '' + row.resultAttempt + ' | ' +
23 | '' + (row.correct === true ? 'YES' : 'NO') + ' |
');
24 | });
25 | });
26 | }
27 |
28 | $(document).ready(function() {
29 |
30 | updateMultiplication();
31 |
32 | $("#attempt-form").submit(function( event ) {
33 |
34 | // Don't submit the form normally
35 | event.preventDefault();
36 |
37 | // Get some values from elements on the page
38 | var a = $('.multiplication-a').text();
39 | var b = $('.multiplication-b').text();
40 | var $form = $( this ),
41 | attempt = $form.find( "input[name='result-attempt']" ).val(),
42 | userAlias = $form.find( "input[name='user-alias']" ).val();
43 |
44 | // Compose the data in the format that the API is expecting
45 | var data = { user: { alias: userAlias}, multiplication: {factorA: a, factorB: b}, resultAttempt: attempt};
46 |
47 | // Send the data using post
48 | $.ajax({
49 | url: '/results',
50 | type: 'POST',
51 | data: JSON.stringify(data),
52 | contentType: "application/json; charset=utf-8",
53 | dataType: "json",
54 | async: false,
55 | success: function(result){
56 | if(result.correct) {
57 | $('.result-message').empty().append("The result is correct! Congratulations!");
58 | } else {
59 | $('.result-message').empty().append("Ooops that's not correct! But keep trying!");
60 | }
61 | }
62 | });
63 |
64 | updateMultiplication();
65 |
66 | updateStats(userAlias);
67 | });
68 | });
69 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/resources/static/styles.css:
--------------------------------------------------------------------------------
1 | html, body {
2 | height: 100%;
3 | }
4 |
5 | html {
6 | display: table;
7 | margin: auto;
8 | }
9 |
10 | body {
11 | display: table-cell;
12 | vertical-align: middle;
13 | }
--------------------------------------------------------------------------------
/social-multiplication/src/test/java/microservices/book/multiplication/controller/MultiplicationControllerTest.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.controller;
2 |
3 | import com.fasterxml.jackson.databind.ObjectMapper;
4 | import microservices.book.multiplication.domain.Multiplication;
5 | import microservices.book.multiplication.service.MultiplicationService;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11 | import org.springframework.boot.test.json.JacksonTester;
12 | import org.springframework.boot.test.mock.mockito.MockBean;
13 | import org.springframework.http.HttpStatus;
14 | import org.springframework.http.MediaType;
15 | import org.springframework.mock.web.MockHttpServletResponse;
16 | import org.springframework.test.context.junit4.SpringRunner;
17 | import org.springframework.test.web.servlet.MockMvc;
18 |
19 | import static org.assertj.core.api.Assertions.assertThat;
20 | import static org.mockito.BDDMockito.given;
21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
22 |
23 | @RunWith(SpringRunner.class)
24 | @WebMvcTest(MultiplicationController.class)
25 | public class MultiplicationControllerTest {
26 |
27 | @MockBean
28 | private MultiplicationService multiplicationService;
29 |
30 | @Autowired
31 | private MockMvc mvc;
32 |
33 | // This object will be magically initialized by the initFields method below.
34 | private JacksonTester