-
Notifications
You must be signed in to change notification settings - Fork 92
/
AlbumController.cs
94 lines (79 loc) · 3.24 KB
/
AlbumController.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
using ContosoMoments.Common.Models;
using Microsoft.Azure.Mobile.Server;
using System;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;
namespace ContosoMoments.Api
{
public class AlbumController : TableController<Album>
{
private MobileServiceContext dbContext;
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
dbContext = new MobileServiceContext();
controllerContext.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
DomainManager = new EntityDomainManager<Album>(dbContext, Request, enableSoftDelete: IsSoftDeleteEnabled());
}
[Route("tables/Album")]
public async Task<IQueryable<Album>> GetAllAlbum()
{
string currentUserId = new ConfigModel().DefaultUserId;
try {
currentUserId = await ManageUserController.GetUserId(Request, User);
}
catch (Exception e) {
Trace.WriteLine("Invalid auth token: " + e);
}
return Query().Where(x => x.UserId == currentUserId || x.IsDefault);
}
[Route("tables/Album/{id}", Name = "GetAlbumById")]
public SingleResult<Album> GetAlbum(string id)
{
return Lookup(id);
}
// PATCH tables/Album/48D68C86-6EA6-4C25-AA33-223FC9A27959
[Authorize]
[Route("tables/Album/{id}")]
public Task<Album> PatchAlbum(string id, Delta<Album> patch)
{
return UpdateAsync(id, patch);
}
// POST tables/Album
[Authorize]
[Route("tables/Album")]
public async Task<IHttpActionResult> PostAlbum(Album item)
{
Album current = await InsertAsync(item);
return CreatedAtRoute("GetAlbumById", new { id = current.Id }, current);
}
// DELETE tables/Album/48D68C86-6EA6-4C25-AA33-223FC9A27959
[Authorize]
[Route("tables/Album/{albumId}")]
public async Task DeleteAlbum(string albumId)
{
if (albumId == new ConfigModel().DefaultAlbumId) {
var message =
new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) { ReasonPhrase = "Cannot delete default album" };
throw new HttpResponseException(message);
}
var album = await dbContext.Albums.FindAsync(albumId);
var domainManager = new EntityDomainManager<Image>(dbContext, Request, IsSoftDeleteEnabled());
foreach (var img in album.Images) {
await domainManager.DeleteAsync(img.Id);
await ImageController.DeleteBlobAsync(img.Id);
}
await DeleteAsync(albumId);
}
public static bool IsSoftDeleteEnabled()
{
return Convert.ToBoolean(ConfigurationManager.AppSettings["enableSoftDelete"]);
}
}
}