-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathCommentsController.cs
More file actions
93 lines (78 loc) · 2.5 KB
/
Copy pathCommentsController.cs
File metadata and controls
93 lines (78 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using BlogEngine.Core.Data.Contracts;
using BlogEngine.Core.Data.Models;
using BlogEngine.Core.Data.ViewModels;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class CommentsController : ApiController
{
readonly ICommentsRepository repository;
public CommentsController(ICommentsRepository repository)
{
this.repository = repository;
}
public CommentsVM Get()
{
return repository.Get();
}
public HttpResponseMessage Get(string id)
{
var result = repository.FindById(new Guid(id));
if (result == null)
return Request.CreateResponse(HttpStatusCode.NotFound);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
public HttpResponseMessage Post([FromBody]CommentDetail item)
{
var result = repository.Add(item);
if (result == null)
return Request.CreateResponse(HttpStatusCode.NotModified);
return Request.CreateResponse(HttpStatusCode.Created, result);
}
public HttpResponseMessage Put([FromBody]CommentItem item)
{
repository.Update(item, "update");
return Request.CreateResponse(HttpStatusCode.OK);
}
public HttpResponseMessage Delete(string id)
{
Guid gId;
if (Guid.TryParse(id, out gId))
repository.Remove(gId);
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpPut]
public HttpResponseMessage ProcessChecked([FromBody]List<CommentItem> items)
{
if (items == null || items.Count == 0)
throw new HttpResponseException(HttpStatusCode.ExpectationFailed);
var action = Request.GetRouteData().Values["id"].ToString().ToLowerInvariant();
foreach (var item in items)
{
if (item.IsChecked)
{
if (action == "delete")
{
repository.Remove(item.Id);
}
else
{
repository.Update(item, action);
}
}
}
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpPut]
public HttpResponseMessage DeleteAll([FromBody]CommentItem item)
{
var action = Request.GetRouteData().Values["id"].ToString();
if (action.ToLower() == "pending" || action.ToLower() == "spam")
{
repository.DeleteAll(action);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
}