Advertisement
Guest User

HumansController

a guest
Jan 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Reflection;
  9. using System.Web.Http;
  10. using WebServiceProva.App_Data;
  11.  
  12. namespace WebServiceProva.Controllers
  13. {
  14.     public class HumansController : ApiController
  15.     {
  16.         string WSPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs.txt");
  17.  
  18.         [HttpPost]
  19.         public HttpResponseMessage InsertHuman(string name, string surname, int age)
  20.         {
  21.             using(TestDBEntities db = new TestDBEntities())
  22.             {
  23.                 try
  24.                 {
  25.                     Humans human = new Humans
  26.                     {
  27.                         Name = name,
  28.                         Surname = surname,
  29.                         Age = age
  30.                     };
  31.                     db.Humans.Add(human);
  32.                     db.SaveChanges();
  33.                     return Request.CreateResponse(HttpStatusCode.OK, human.ID);
  34.                 }
  35.                 catch(Exception ex)
  36.                 {
  37.                     LogToFile(ex.ToString());
  38.                     return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
  39.                 }
  40.             }
  41.         }
  42.  
  43.         [HttpGet]
  44.         public HttpResponseMessage GetHumanByID(int id)
  45.         {
  46.             using (TestDBEntities db = new TestDBEntities())
  47.             {
  48.                 try
  49.                 {
  50.                     Humans human = (from a in db.Humans where a.ID == id select a).FirstOrDefault() ?? throw new Exception("Human " + id + " not found.");
  51.                     return Request.CreateResponse(HttpStatusCode.OK, human);
  52.                 }
  53.                 catch (Exception ex)
  54.                 {
  55.                     LogToFile(ex.ToString());
  56.                     return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
  57.                 }
  58.             }
  59.         }
  60.  
  61.         [HttpDelete]
  62.         public HttpResponseMessage DeleteHumanByID(int id)
  63.         {
  64.             using (TestDBEntities db = new TestDBEntities())
  65.             {
  66.                 try
  67.                 {
  68.                     int done = db.Database.ExecuteSqlCommand(
  69.                         "DELETE FROM Humans WHERE ID = @id",
  70.                         new SqlParameter("@id", id)
  71.                         );
  72.  
  73.                     if(done == 0)
  74.                     {
  75.                         throw new Exception("Human " + id + " not found.");
  76.                     }
  77.                     else
  78.                     {
  79.                         return Request.CreateResponse(HttpStatusCode.OK, "Human " + id + " deleted.");
  80.                     }
  81.                 }
  82.                 catch (Exception ex)
  83.                 {
  84.                     LogToFile(ex.ToString());
  85.                     return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
  86.                 }
  87.             }
  88.         }
  89.  
  90.         [HttpPut]
  91.         public HttpResponseMessage UpdateHumanByID(int id, string name, string surname, int age)
  92.         {
  93.             using (TestDBEntities db = new TestDBEntities())
  94.             {
  95.                 try
  96.                 {
  97.                     int done = db.Database.ExecuteSqlCommand(
  98.                         "UPDATE Humans SET Name = @name, Surname = @surname, Age = @age WHERE ID = @id",
  99.                         new SqlParameter("@id", id),
  100.                         new SqlParameter("@name", name),
  101.                         new SqlParameter("@surname", surname),
  102.                         new SqlParameter("@age", age)
  103.                         );
  104.  
  105.                     if (done == 0)
  106.                     {
  107.                         throw new Exception("Human " + id + " not found.");
  108.                     }
  109.                     else
  110.                     {
  111.                         return Request.CreateResponse(HttpStatusCode.OK, "Human " + id + " updated.");
  112.                     }
  113.                 }
  114.                 catch (Exception ex)
  115.                 {
  116.                     LogToFile(ex.ToString());
  117.                     return Request.CreateResponse(HttpStatusCode.InternalServerError, "An error has occurred.");
  118.                 }
  119.             }
  120.         }
  121.  
  122.         private void LogToFile(string message)
  123.         {
  124.             File.AppendAllText(WSPath, DateTime.Now + " - " + message + Environment.NewLine);
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement