-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathPackagesController.cs
More file actions
162 lines (142 loc) · 4.63 KB
/
Copy pathPackagesController.cs
File metadata and controls
162 lines (142 loc) · 4.63 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using BlogEngine.Core.Data.Contracts;
using BlogEngine.Core.Data.Models;
using BlogEngine.Core.Packaging;
using System.Collections.Generic;
using System.Web.Http;
using System;
using System.Net;
using System.Net.Http;
public class PackagesController : ApiController
{
readonly IPackageRepository repository;
public PackagesController(IPackageRepository repository)
{
this.repository = repository;
}
public IEnumerable<Package> Get(int take = 10, int skip = 0, string filter = "", string order = "")
{
return repository.Find(take, skip, filter, order);
}
public HttpResponseMessage Get(string id)
{
var result = repository.FindById(id);
if (result == null)
return Request.CreateResponse(HttpStatusCode.NotFound);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
[HttpPut]
public HttpResponseMessage ProcessChecked([FromBody]List<Package> items)
{
if (items == null || items.Count == 0)
throw new HttpResponseException(HttpStatusCode.ExpectationFailed);
var action = Request.GetRouteData().Values["id"].ToString();
try
{
if (action == "install")
{
foreach (var item in items)
{
if (!repository.Install(item.Id))
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
if (action == "uninstall")
{
foreach (var item in items)
{
if (!repository.Uninstall(item.Id))
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
if (action == "enable")
{
foreach (var item in items)
{
item.Enabled = true;
repository.Update(item);
}
}
if (action == "disable")
{
foreach (var item in items)
{
item.Enabled = false;
repository.Update(item);
}
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception ex)
{
BlogEngine.Core.Utils.Log("Error processing package", ex);
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
[HttpPut]
public bool Update([FromBody]Package item)
{
try
{
repository.Update(item);
return true;
}
catch (UnauthorizedAccessException)
{
throw new HttpResponseException(HttpStatusCode.Unauthorized);
}
catch (Exception ex)
{
BlogEngine.Core.Utils.Log("Error updating package", ex);
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
[HttpPut]
public HttpResponseMessage Install(string id)
{
if (string.IsNullOrEmpty(id))
throw new HttpResponseException(HttpStatusCode.ExpectationFailed);
repository.Install(id);
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpPut]
public HttpResponseMessage Uninstall(string id)
{
if (string.IsNullOrEmpty(id))
throw new HttpResponseException(HttpStatusCode.ExpectationFailed);
repository.Uninstall(id);
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpPut]
public HttpResponseMessage Rate(string id, [FromBody]Review review)
{
if (review == null) return Request.CreateResponse(HttpStatusCode.ExpectationFailed, "Review can not be null");
if (review.Body.Length > 450) review.Body = review.Body.Substring(0, 450);
var result = Gallery.RatePackage(id, review);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
[HttpPut]
public bool Refresh()
{
try
{
BlogEngine.Core.Blog.CurrentInstance.Cache.Remove(Constants.CacheKey);
return true;
}
catch (Exception ex)
{
BlogEngine.Core.Utils.Log("Error refreshing gallery cache", ex);
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
[HttpPut]
public HttpResponseMessage SetTheme(string id)
{
if (!string.IsNullOrEmpty(id))
{
var bs = BlogEngine.Core.BlogSettings.Instance;
bs.Theme = id;
bs.Save();
}
return Request.CreateResponse(HttpStatusCode.OK);
}
}