4
What would be the best and simplest alternative?
I have the Web Api:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Generico.Dominio;
using Generico.Aplicacao;
namespace AspNetWebApi.Controllers
{
[RoutePrefix("api/AspNetWebApi")]
public class DefaultController : ApiController
{
//http://localhost:7630/api/AspNetWebApi/consulta/JogosPorID/5
[HttpGet]
[Route("consulta/JogosPorID/{id:int}")]
public HttpResponseMessage JogosPorID(int id)
{
try
{
var tTabela = new JogoDetalheAplicacao();
var listar = tTabela.ListarTodos(id);
return Request.CreateResponse(HttpStatusCode.OK, listar.ToArray());
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
//http://localhost:7630/api/AspNetWebApi/cadastrar/jogo/4512/20.01/20.10/5
[HttpPost]
[Route("cadastrar/jogo/{nJogo}/{valor}/{total}/{idusuario}")]
public HttpResponseMessage Cadastro(int nJogo, decimal valor, decimal total, int idusuario)
{
try
{
var tTabela = new JogoDetalheAplicacao();
tTabela.Inseri(nJogo, valor,total,idusuario);
return Request.CreateResponse(HttpStatusCode.OK, "Cadastro realizado.");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
//http://localhost:7630/api/AspNetWebApi/deletar/jogo/4512/5
//precisa usar o postman com opção delete formato json
[HttpDelete]
[Route("deletar/jogo/{nJogo}/{idusuario}")]
public HttpResponseMessage Deletar(int nJogo, int idusuario)
{
try
{
var tTabela = new JogoDetalheAplicacao();
var resultado = tTabela.Excluir(nJogo, idusuario);
return Request.CreateResponse(HttpStatusCode.OK, resultado);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
//http://localhost:1608/api/ApiGuiaCidade/datahora/consulta
[HttpGet]
[Route("datahora/consulta")]
public HttpResponseMessage GetDataHoraServidor()
{
try
{
var dataHora = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
return Request.CreateResponse(HttpStatusCode.OK, dataHora);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
}
}
}
I have the Script:
(function () {
'use strict';
var numeros = angular
.module("myModule", [])
.controller("myController", function ($scope, $http, $log) {
var sv = this;
var sucessoCalBack = function (response) {
$scope.detalhes = response.data;
};
var erroCalBack = function (response) {
$scope.error = response.data;
};
//assim funciona, passando o parametro direto
$http({
method: 'GET',
params: { idusuario: 5 },
url: 'http://localhost:7630/api/AspNetWebApi/consulta/JogosPorID/5'})
.then(sucessoCalBack,erroCalBack);
});
sv.getAll = function (idusuario, onSuccess, onFail) {
var config = {
method: 'GET',
url: 'http://localhost:7630/api/AspNetWebApi/consulta/JogosPorID/',
params: { idusuario:idusuario }
};
$http(config)
.then(function (response) {
onSuccess(response.data);
}, function (response) {
onFail(response.statusText);
});
};
//Inser Detalhe
sv.post = function (nJogo, valor, total, idusuario, onSuccess, onFail) {
var config = {
method: 'POST',
url: 'http://localhost:7630/api/AspNetWebApi/cadastrar/jogo/',
params: { nJogo: nJogo, valor: valor, total: total, idusuario: idusuario }
};
$http(config)
.then(function (response) {
onSuccess(response.data);
}, function (response) {
onFail(response.statusText);
});
};
//Delete detalhe
sv.delete = function (idusuario,numerojogo , onSuccess, onFail) {
var config = {
method: 'DELETE',
url: 'http://localhost:7630/api/AspNetWebApi/deletar/jogo/',
params: { idusuario: idusuario, numerojogo: numerojogo }
}
$http(config)
.then(function (response) {
onSuccess(response.data);
}, function (response) {
onFail(response.statusText);
});
};
})();
Doubts:
1 - I need to do Get by passing a parameter that would be the User ID
2 - I need to post and delete also sending the User ID is game number
I am providing the complete project, has only 1 table (with script already with data), in case someone can help me learn this, I am using VS 2015, I thank you.
https://drive.google.com/folderview?id=0B-svO0-4L_-NZF9pWFVwV3p4Wms&usp=sharing
Have questions, so I can’t send the data directly to the controller? , I just need to make a data post : number is value, how could I make this serve?
– Harry
@itasouza updated my answer by adding some information on how to controllers and services should be used and how to use the service of the example in a controller. I hope this makes it clear to you. :)
– Zignd
I will create a service and prepare it to receive the data, so get ready if you have questions I return the question, I really appreciate your help.
– Harry
I set up the service, I did the tests is until I can return the data, more I can not follow, see I changed my code, I also posted the project that only has 1 table, would you help me to do this? thanks
– Harry
@itasouza To make a GET you pass the data by parameter using the property
params
in the configuration object of service$http
, take a look at my example of service and look for the methodsv.getById
. About the POST, you pass the data using the propertydata
in the configuration object of service$http
, take a look at my example and look for the methodsv.post
.– Zignd
I made the adjustments, posted the code and added a copy in Google Drive, see if it is correct, see if anything is missing, I appreciate the help!
– Harry
I’m uploading a copy of the project, the link is at the end of the question, in google Drive, only it will take about 10 minutes, it’s going up is slow today.
– Harry
@itasouza From what I saw here is right, I would only make some changes to the code as perfumery, but the way it is seems to be working properly.
– Zignd
@itasouza No need, I ended up seeing after that you also put in question the code. xD
– Zignd
@itasouza If you are interested, this article here is a style guide for you to format your code in Angularjs in the best way possible, so that it is easier to maintain in the future. https://github.com/johnpapa/angular-styleguide
– Zignd
I still have doubts, it is still difficult to understand, so if possible do there adjustments, I changed the get see if it will work when I send the Angular parameter, in this case I will need to have this project separate just to keep receiving the angular data? I’ll see the article
– Harry
Do you have any way to test Sv.getAll?
– Harry
Well, you can test it using a button in the application, then you use the Directive
ng-click
and passes on to it the call of a function of controller. Type,ng-click="ctrl.salvarProduto()"
.– Zignd
i needed an example more within the reality of the code if possible: <button class="btn btn-Primary btn-block " ng-click="myController.getAll(5)">List Data</button>
– Harry