This is first time I have tried to make an API using MVC4. So far it is working ok. For practice I want to try and send my model using a queryString in the GET method instead of my current POST send method.
As I have no clue how to start, and whats need to be done ? Can you come with some advices ?
Can I have the Index view as it is ? or anything need to be changed ?
I also would like to keep the curent GET method I have to show the list, I guess that will interfere if I add a new GET method fot sending querystring ?
All advices is helpful here ^_^
namespace MessageHandler.Models
{
public class Message
{
public int Id { get; set; }
public double Amount { get; set; }
public string PaymentMessage { get; set; }
public string Name { get; set; }
public string Adress { get; set; }
}
}
namespace MessageHandler.Services
{
public class MessageRepository
{
private const string CacheKey = "MessageStore";
public MessageRepository()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
if (ctx.Cache[CacheKey] == null)
{
var messages = new Message[]
{
new Message
{
Id = 1, PaymentMessage = "payment for order 23434",
Name = "Christer Glenning", Adress = "Grenvagen 24",
Amount = 2300
},
new Message
{
Id = 2, PaymentMessage = "payment for order 24354",
Name = "Carl Ingemar", Adress = "Regnbagen 446",
Amount = 44
}
};
ctx.Cache[CacheKey] = messages;
}
}
}
public Message[] GetAllMessages()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
return (Message[])ctx.Cache[CacheKey];
}
return new Message[]
{
new Message
{
Id = 0,
Name = "placeHolder"
}
};
}
public bool SaveMessage(Message message)
{
var ctx = HttpContext.Current;
if (ctx != null)
{
try
{
var currentData = ((Message[])ctx.Cache[CacheKey]).ToList();
currentData.Add(message);
ctx.Cache[CacheKey] = currentData.ToArray();
return true;
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
return false;
}
}
return false;
}
}
}
using MessageHandler.Models;
using MessageHandler.Services;
namespace MessageHandler.Controllers
{
public class MessageController : ApiController
{
private MessageRepository repo;
public MessageController()
{
this.repo = new MessageRepository();
}
public Message[] Get()
{
return repo.GetAllMessages();
}
public HttpResponseMessage Post(Message message)
{
this.repo.SaveMessage(message);
var response = Request.CreateResponse<Message>(System.
Net.HttpStatusCode.Created, message);
return response;
}
}
}
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
<a href="~/">API</a></p>
</div>
</div>
</header>
<div id="body">
<ul id="messages"></ul>
<form id="saveMessageForm" method="post">
<h3>Create a new Message</h3>
<p>
<label for="messageId">Id:</label>
<input type="text" name="Id" />
</p>
<p>
<label for="messagePaymentMessage">Message:</label>
<input type="text" name="PaymentMessage" />
</p>
<p>
<label for="messageName">Name:</label>
<input type="text" name="Name" />
</p>
<p>
<label for="messageAdress">Adress:</label>
<input type="text" name="Adress" />
</p>
<p>
<label for="messageAmount">Amount:</label>
<input type="text" name="Amount" />
</p>
<input type="button" id="saveMessage" value="Save" />
</form>
</div>
@section scripts
{
<script type="text/javascript">
$(function()
{
$.getJSON('/api/message', function (messagesJsonPayload) {
$(messagesJsonPayload).each(function (i, item) {
$('#messages').append('<li>' + item.PaymentMessage + '</li>' +
'<li>' + item.Name + '</li>' +
'<li>' + item.Adress + '</li>' +
'<li>' + '$' + item.Amount.toFixed(2) + '</li>' + '<br>');
});
});
});
</script>
<script type="text/javascript">
$('#saveMessage').click(function()
{
$.post("api/message",
$("#saveMessageForm").serialize(),
function(value)
{
$('#messages').append('<li>' + value.PaymentMessage + '</li>' +
'<li>' + value.Name + '</li>' +
'<li>' + value.Adress + '</li>' +
'<li>' + '$' + value.Amount.toFixed(2) + '</li>');
},
"json"
);
});
</script>
}