Advertisement
Guest User

Untitled

a guest
Aug 14th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7.  
  8. namespace TestTask.Controllers
  9. {
  10.     public class TaskController : ApiController
  11.     {
  12.         private DbClassesDataContext dbContext { get; set; }
  13.  
  14.         public TaskController()
  15.         {
  16.             dbContext = new DbClassesDataContext();
  17.         }
  18.  
  19.         [HttpGet]
  20.         public IHttpActionResult FetchAll()
  21.         {
  22.             return Json(dbContext.Tasks);
  23.         }
  24.  
  25.         [HttpPost]
  26.         public IHttpActionResult CreateNew(int? taskType, string taskName = null)
  27.         {
  28.             if (taskName != String.Empty && taskName != null)
  29.             {
  30.                 if (taskType.HasValue)
  31.                 {
  32.                     if (taskType >= 0 && taskType < 4)
  33.                     {
  34.                         var task = new Task
  35.                         {
  36.                             name = taskName,
  37.                             type = taskType.Value,
  38.                             done_percent = 0,
  39.                             creation_date = DateTime.Now,
  40.                             last_date = DateTime.Now
  41.                         };
  42.  
  43.                         dbContext.Tasks.InsertOnSubmit(task);
  44.                         dbContext.SubmitChanges();
  45.  
  46.                         return Json(new Models.Message
  47.                         {
  48.                             Id = 0,
  49.                             Text = "The new task was created.",
  50.                             Time = DateTime.Now
  51.                         });
  52.                     }
  53.                     else
  54.                     {
  55.                         return Json(new Models.Error
  56.                         {
  57.                             Id = 2,
  58.                             Message = "The task type can have only values in range: [0, 3].",
  59.                             Time = DateTime.Now
  60.                         });
  61.                     }
  62.                 }
  63.                 else
  64.                 {
  65.                     return Json(new Models.Error
  66.                     {
  67.                         Id = 1,
  68.                         Message = "The task type can't be null.",
  69.                         Time = DateTime.Now
  70.                     });
  71.                 }
  72.             }
  73.             else
  74.                 return Json(new Models.Error
  75.                 {
  76.                     Id = 0,
  77.                     Message = "The task name was empty.",
  78.                     Time = DateTime.Now
  79.                 });
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement