Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div ng-controller="uploadController" >
- <div class="form-group col-md-12" >
- <div class="form-group">
- <input type="file" fileinput="file" filepreview="filepreview"/>
- </div>
- </div>
- <div class=" form-group col-md-6">
- <img ng-src="{{filepreview}}" class="img-responsive" ng-show="filepreview" />
- </div>
- </div>
- app.controller(
- "uploadController",
- [ '$scope', '$http', 'uploadService',
- function($scope, $http, uploadService) {
- $scope.$watch('file', function(newfile, oldfile) {
- if (angular.equals(newfile, oldfile)) {
- return;
- }
- uploadService.upload(newfile).then(function(res) {
- console.log("result", res);
- })
- });
- } ]).service("uploadService", function($http, $q) {
- return ({
- upload : upload
- });
- function upload(file) {
- var upl = $http({
- method : 'POST',
- url : 'http://jsonplaceholder.typicode.com/posts', // /api/upload
- headers : {
- 'Content-Type' : 'multipart/form-data'
- },
- data : {
- upload : file
- },
- transformRequest : function(data, headersGetter) {
- var formData = new FormData();
- angular.forEach(data, function(value, key) {
- formData.append(key, value);
- });
- var headers = headersGetter();
- delete headers['Content-Type'];
- return formData;
- }
- });
- return upl.then(handleSuccess, handleError);
- }
- function handleError(response, data) {
- if (!angular.isObject(response.data) || !response.data.message) {
- return ($q.reject("An unknown error occurred."));
- }
- return ($q.reject(response.data.message));
- }
- function handleSuccess(response) {
- return (response);
- }
- }).directive("fileinput", [ function() {
- return {
- scope : {
- fileinput : "=",
- filepreview : "="
- },
- link : function(scope, element, attributes) {
- element.bind("change", function(changeEvent) {
- scope.fileinput = changeEvent.target.files[0];
- var reader = new FileReader();
- reader.onload = function(loadEvent) {
- scope.$apply(function() {
- scope.filepreview = loadEvent.target.result;
- });
- }
- reader.readAsDataURL(scope.fileinput);
- });
- }
- }
- } ]);
- @Entity
- public class Promocao {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- private String produto;
- private double preco;
- private String marca;
- private String categoria;
- private String uniMedida;
- private String observacao;
- @Column(name = "dataInicio")
- @Temporal(TemporalType.DATE)
- private Date dataInicio;
- @Column(name = "dataFim")
- @Temporal(TemporalType.DATE)
- private Date dataFim;
- private String status;
- @JoinColumn
- @ManyToOne(cascade = CascadeType.REFRESH)
- private Usuario usuario;
- private String foto; //aqui entraria o endereço da imagem
- gets .. sets....
- @RestController
- @RequestMapping
- public class PromocaoController {
- @Autowired
- PromocaoService promocaoService;
- //end points
- @RequestMapping(method=RequestMethod.POST, value="/promocoes", consumes=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Promocao> cadastrarPromocao(@RequestBody Promocao promocao){
- Promocao promocaoCadastrada = promocaoService.cadastrar(promocao);
- return new ResponseEntity<Promocao>(promocaoCadastrada, HttpStatus.CREATED);
- }
- @RequestMapping(method=RequestMethod.GET, value="/promocoes", produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Collection<Promocao>> buscarTodasPromocoes(){
- Collection<Promocao> promocaoBuscadas= promocaoService.buscarTodos();
- return new ResponseEntity<>(promocaoBuscadas, HttpStatus.OK);
- }
- @RequestMapping(method=RequestMethod.GET, value="/promocoes/{id}", produces=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Promocao> buscarPromocaoPorId(@PathVariable int id){
- Promocao promocao= promocaoService.buscaPorId(id);
- return new ResponseEntity<>(promocao, HttpStatus.OK);
- }
- @RequestMapping(method=RequestMethod.DELETE, value="/promocoes/{id}")
- public ResponseEntity<Promocao> excluirPromocao(@PathVariable int id){
- Promocao promocaoEncontrada = promocaoService.buscaPorId(id);
- if(promocaoEncontrada == null){
- return new ResponseEntity<>(HttpStatus.NOT_FOUND);
- }
- promocaoService.excluir(promocaoEncontrada);
- return new ResponseEntity<>(HttpStatus.OK);
- }
- @RequestMapping(method=RequestMethod.PUT, value="/promocoes", consumes=MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<Promocao> alterarPromocao(@RequestBody Promocao promocao){
- Promocao promocaoAlterada = promocaoService.alterar(promocao);
- return new ResponseEntity<Promocao>(promocaoAlterada, HttpStatus.OK);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement