Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. <div ng-controller="uploadController" >
  2. <div class="form-group col-md-12" >
  3. <div class="form-group">
  4. <input type="file" fileinput="file" filepreview="filepreview"/>
  5.  
  6. </div>
  7. </div>
  8. <div class=" form-group col-md-6">
  9. <img ng-src="{{filepreview}}" class="img-responsive" ng-show="filepreview" />
  10. </div>
  11. </div>
  12.  
  13. app.controller(
  14. "uploadController",
  15. [ '$scope', '$http', 'uploadService',
  16. function($scope, $http, uploadService) {
  17. $scope.$watch('file', function(newfile, oldfile) {
  18. if (angular.equals(newfile, oldfile)) {
  19. return;
  20. }
  21.  
  22. uploadService.upload(newfile).then(function(res) {
  23. console.log("result", res);
  24. })
  25. });
  26.  
  27. } ]).service("uploadService", function($http, $q) {
  28.  
  29. return ({
  30. upload : upload
  31. });
  32.  
  33. function upload(file) {
  34. var upl = $http({
  35. method : 'POST',
  36. url : 'http://jsonplaceholder.typicode.com/posts', // /api/upload
  37. headers : {
  38. 'Content-Type' : 'multipart/form-data'
  39. },
  40. data : {
  41. upload : file
  42. },
  43. transformRequest : function(data, headersGetter) {
  44. var formData = new FormData();
  45. angular.forEach(data, function(value, key) {
  46. formData.append(key, value);
  47. });
  48.  
  49. var headers = headersGetter();
  50. delete headers['Content-Type'];
  51.  
  52. return formData;
  53. }
  54. });
  55. return upl.then(handleSuccess, handleError);
  56.  
  57. }
  58.  
  59. function handleError(response, data) {
  60. if (!angular.isObject(response.data) || !response.data.message) {
  61. return ($q.reject("An unknown error occurred."));
  62. }
  63.  
  64. return ($q.reject(response.data.message));
  65. }
  66.  
  67. function handleSuccess(response) {
  68. return (response);
  69. }
  70.  
  71. }).directive("fileinput", [ function() {
  72. return {
  73. scope : {
  74. fileinput : "=",
  75. filepreview : "="
  76. },
  77. link : function(scope, element, attributes) {
  78. element.bind("change", function(changeEvent) {
  79. scope.fileinput = changeEvent.target.files[0];
  80. var reader = new FileReader();
  81. reader.onload = function(loadEvent) {
  82. scope.$apply(function() {
  83. scope.filepreview = loadEvent.target.result;
  84. });
  85. }
  86. reader.readAsDataURL(scope.fileinput);
  87. });
  88. }
  89. }
  90. } ]);
  91.  
  92. @Entity
  93. public class Promocao {
  94.  
  95. @Id
  96. @GeneratedValue(strategy = GenerationType.IDENTITY)
  97. private int id;
  98. private String produto;
  99. private double preco;
  100. private String marca;
  101. private String categoria;
  102. private String uniMedida;
  103. private String observacao;
  104.  
  105.  
  106. @Column(name = "dataInicio")
  107. @Temporal(TemporalType.DATE)
  108. private Date dataInicio;
  109.  
  110.  
  111. @Column(name = "dataFim")
  112. @Temporal(TemporalType.DATE)
  113. private Date dataFim;
  114.  
  115. private String status;
  116.  
  117. @JoinColumn
  118. @ManyToOne(cascade = CascadeType.REFRESH)
  119. private Usuario usuario;
  120.  
  121. private String foto; //aqui entraria o endereço da imagem
  122.  
  123. gets .. sets....
  124.  
  125. @RestController
  126. @RequestMapping
  127. public class PromocaoController {
  128.  
  129. @Autowired
  130. PromocaoService promocaoService;
  131.  
  132.  
  133. //end points
  134.  
  135.  
  136. @RequestMapping(method=RequestMethod.POST, value="/promocoes", consumes=MediaType.APPLICATION_JSON_VALUE)
  137. public ResponseEntity<Promocao> cadastrarPromocao(@RequestBody Promocao promocao){
  138. Promocao promocaoCadastrada = promocaoService.cadastrar(promocao);
  139. return new ResponseEntity<Promocao>(promocaoCadastrada, HttpStatus.CREATED);
  140. }
  141.  
  142.  
  143. @RequestMapping(method=RequestMethod.GET, value="/promocoes", produces=MediaType.APPLICATION_JSON_VALUE)
  144. public ResponseEntity<Collection<Promocao>> buscarTodasPromocoes(){
  145.  
  146. Collection<Promocao> promocaoBuscadas= promocaoService.buscarTodos();
  147. return new ResponseEntity<>(promocaoBuscadas, HttpStatus.OK);
  148. }
  149.  
  150. @RequestMapping(method=RequestMethod.GET, value="/promocoes/{id}", produces=MediaType.APPLICATION_JSON_VALUE)
  151. public ResponseEntity<Promocao> buscarPromocaoPorId(@PathVariable int id){
  152.  
  153. Promocao promocao= promocaoService.buscaPorId(id);
  154. return new ResponseEntity<>(promocao, HttpStatus.OK);
  155. }
  156.  
  157.  
  158. @RequestMapping(method=RequestMethod.DELETE, value="/promocoes/{id}")
  159. public ResponseEntity<Promocao> excluirPromocao(@PathVariable int id){
  160. Promocao promocaoEncontrada = promocaoService.buscaPorId(id);
  161. if(promocaoEncontrada == null){
  162. return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  163. }
  164.  
  165. promocaoService.excluir(promocaoEncontrada);
  166. return new ResponseEntity<>(HttpStatus.OK);
  167. }
  168.  
  169. @RequestMapping(method=RequestMethod.PUT, value="/promocoes", consumes=MediaType.APPLICATION_JSON_VALUE)
  170. public ResponseEntity<Promocao> alterarPromocao(@RequestBody Promocao promocao){
  171. Promocao promocaoAlterada = promocaoService.alterar(promocao);
  172. return new ResponseEntity<Promocao>(promocaoAlterada, HttpStatus.OK);
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement