Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Reflection;
- using System.Web.Http;
- using WebServiceProva.App_Data;
- namespace WebServiceProva.Controllers
- {
- public class HumansController : ApiController
- {
- string WSPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs.txt");
- [HttpPost]
- public HttpResponseMessage InsertHuman(string name, string surname, int age)
- {
- using(TestDBEntities db = new TestDBEntities())
- {
- try
- {
- Humans human = new Humans
- {
- Name = name,
- Surname = surname,
- Age = age
- };
- db.Humans.Add(human);
- db.SaveChanges();
- return Request.CreateResponse(HttpStatusCode.OK, human.ID);
- }
- catch(Exception ex)
- {
- LogToFile(ex.ToString());
- return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
- }
- }
- }
- [HttpGet]
- public HttpResponseMessage GetHumanByID(int id)
- {
- using (TestDBEntities db = new TestDBEntities())
- {
- try
- {
- Humans human = (from a in db.Humans where a.ID == id select a).FirstOrDefault() ?? throw new Exception("Human " + id + " not found.");
- return Request.CreateResponse(HttpStatusCode.OK, human);
- }
- catch (Exception ex)
- {
- LogToFile(ex.ToString());
- return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
- }
- }
- }
- [HttpDelete]
- public HttpResponseMessage DeleteHumanByID(int id)
- {
- using (TestDBEntities db = new TestDBEntities())
- {
- try
- {
- int done = db.Database.ExecuteSqlCommand(
- "DELETE FROM Humans WHERE ID = @id",
- new SqlParameter("@id", id)
- );
- if(done == 0)
- {
- throw new Exception("Human " + id + " not found.");
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.OK, "Human " + id + " deleted.");
- }
- }
- catch (Exception ex)
- {
- LogToFile(ex.ToString());
- return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
- }
- }
- }
- [HttpPut]
- public HttpResponseMessage UpdateHumanByID(int id, string name, string surname, int age)
- {
- using (TestDBEntities db = new TestDBEntities())
- {
- try
- {
- int done = db.Database.ExecuteSqlCommand(
- "UPDATE Humans SET Name = @name, Surname = @surname, Age = @age WHERE ID = @id",
- new SqlParameter("@id", id),
- new SqlParameter("@name", name),
- new SqlParameter("@surname", surname),
- new SqlParameter("@age", age)
- );
- if (done == 0)
- {
- throw new Exception("Human " + id + " not found.");
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.OK, "Human " + id + " updated.");
- }
- }
- catch (Exception ex)
- {
- LogToFile(ex.ToString());
- return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
- }
- }
- }
- private void LogToFile(string message)
- {
- File.AppendAllText(WSPath, DateTime.Now + " - " + message + Environment.NewLine);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement