Skip to content
Snippets Groups Projects

Merge 'develop' changes into 'master'.

Merged BEAUVAIS ANTOINE requested to merge develop into master
Viewing commit 9b5986d4
Show latest version
4 files
+ 94
8
Preferences
Compare changes
Files
4
@@ -4,10 +4,18 @@
*/
package fr.unistra.sil.erp.back.controller.api;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import static fr.unistra.sil.erp.back.Config.MAPPING_SUBTRANSAC;
import fr.unistra.sil.erp.back.controller.ISubmitTransactionController;
import fr.unistra.sil.erp.back.model.Transaction;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -22,10 +30,44 @@ public class ApiSubmitTransactionController
implements ISubmitTransactionController {
@RequestMapping(value=MAPPING_SUBTRANSAC, method = RequestMethod.POST)
@Override
public ResponseEntity<Object> submitTransaction(HttpServletRequest request,
HttpServletResponse response)
HttpServletResponse response) throws ApiBadRequestException
{
throw new UnsupportedOperationException("Not yet supported.");
Gson gson = new Gson();
String body;
try {
body = request.getReader().lines()
.collect(Collectors.joining(System.lineSeparator()));
} catch (IOException ex) {
Logger.getLogger(ApiSubmitTransactionController.class.getName())
.log(Level.SEVERE, "Unparseable body.", ex);
throw new ApiBadRequestException("Unparseable body.");
}
if(body == null)
throw new ApiBadRequestException("Missing JSON body.");
Transaction t;
try
{
t = gson.fromJson(body, Transaction.class);
}
catch(JsonParseException ex)
{
throw new ApiBadRequestException("Invalid JSON: syntax error.");
}
if(t == null)
throw new ApiBadRequestException("Missing JSON body.");
if(t.getItem() == null || t.getType() == null ||
t.getAmount() == null)
throw new ApiBadRequestException("Invalid JSON schema.");
System.out.println("Transaction : " + t.getItem() +
t.getType() + " " + t.getAmount());
return new ResponseEntity<>(t, HttpStatus.CREATED);
}
}