-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBannerController.cs
142 lines (124 loc) · 3.89 KB
/
BannerController.cs
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
using BannerFlow.DataTransferObjects;
using BannerFlow.Models;
using BannerFlow.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
namespace BannerFlow.Controllers
{
[RoutePrefix("api/banners")]
public class BannerController : ApiController
{
private static BannerService bannerService;
public BannerController()
{
bannerService = new BannerService();
}
// GET api/banners
[Route("")]
[HttpGet]
public IEnumerable<BannerDTO> Get()
{
try
{
return bannerService.GetAll();
}
catch
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
// GET api/banners/{id}
[Route("{id:int}")]
[HttpGet]
public BannerDTO Get(int id)
{
try
{
var banner = bannerService.Get(id);
return banner;
}
catch
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
// POST api/banners
[Route("")]
[HttpPost]
public HttpResponseMessage Post(BannerDTO value)
{
HttpResponseMessage result = null;
try
{
var banner = bannerService.Add(value);
result = Request.CreateResponse<Banner>(HttpStatusCode.OK, banner);
string newItemURL = Url.Link("BannerApi", new { id = banner.Id });
result.Headers.Location = new Uri(newItemURL);
}
catch (Exception ex)
{
Trace.TraceError(ex.Message, ex);
result = Request.CreateResponse<string>(HttpStatusCode.InternalServerError, ex.Message);
}
return result;
}
// PUT api/banners/id
[Route("{id:int}")]
[HttpPut]
public HttpResponseMessage Update(int id, BannerDTO value)
{
HttpResponseMessage result = null;
try
{
Banner update = bannerService.Update(id, value);
result = Request.CreateResponse(HttpStatusCode.OK, update);
}
catch (Exception ex)
{
Trace.TraceError(ex.Message, ex);
result = Request.CreateResponse<string>(HttpStatusCode.InternalServerError, ex.Message);
}
return result;
}
// DELETE api/banners/id
[Route("{id:int}")]
[HttpDelete]
public HttpResponseMessage Delete(int id)
{
HttpResponseMessage result = null;
try
{
bannerService.Delete(id);
result = Request.CreateResponse(HttpStatusCode.OK, "Entry was deleted.");
}
catch (Exception ex)
{
Trace.TraceError(ex.Message, ex);
result = Request.CreateResponse<string>(HttpStatusCode.InternalServerError, ex.Message);
}
return result;
}
// GET api/banners/{id}/html
[Route("{id:int}/html")]
public HttpResponseMessage GetHtml(int id)
{
try {
var banner = bannerService.GetHtml(id);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(banner.Html);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
return response;
}
catch
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
}
}