|
@@ -0,0 +1,68 @@
|
|
1
|
+using System.Net;
|
|
2
|
+using System.Net.Http;
|
|
3
|
+using System.Web.Http;
|
|
4
|
+using iiie.Logs.DBO;
|
|
5
|
+
|
|
6
|
+namespace iiie.WebApiUtils.BusinessManager
|
|
7
|
+{
|
|
8
|
+ /// <summary>
|
|
9
|
+ /// Handle business manager results
|
|
10
|
+ /// </summary>
|
|
11
|
+ /// <typeparam name="T">Data type</typeparam>
|
|
12
|
+ public class BMRHandler<T>
|
|
13
|
+ {
|
|
14
|
+ /// <summary>
|
|
15
|
+ /// Converts ResultStatus codes to HttpStatusCodes codes
|
|
16
|
+ /// </summary>
|
|
17
|
+ /// <param name="status">The status code</param>
|
|
18
|
+ /// <returns>The http code</returns>
|
|
19
|
+ public static HttpStatusCode ResultStatusToHttp(ResultStatus status)
|
|
20
|
+ {
|
|
21
|
+ if (status == ResultStatus.LoginError)
|
|
22
|
+ return HttpStatusCode.Unauthorized;
|
|
23
|
+ if (status == ResultStatus.PermissionError)
|
|
24
|
+ return HttpStatusCode.Forbidden;
|
|
25
|
+ if (status == ResultStatus.InputError)
|
|
26
|
+ return HttpStatusCode.BadRequest;
|
|
27
|
+ if (status == ResultStatus.NotFound)
|
|
28
|
+ return HttpStatusCode.NotFound;
|
|
29
|
+ return HttpStatusCode.InternalServerError;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ /// <summary>
|
|
33
|
+ /// Returns a string corresponding to the error
|
|
34
|
+ /// </summary>
|
|
35
|
+ /// <param name="result">The result of the operation</param>
|
|
36
|
+ /// <returns>The error string</returns>
|
|
37
|
+ public static string OpResultToString(OpResult<T> result)
|
|
38
|
+ {
|
|
39
|
+ if (result)
|
|
40
|
+ return result.PublicDetails;
|
|
41
|
+
|
|
42
|
+ if (result.Status == ResultStatus.LoginError)
|
|
43
|
+ return "Bad username/password";
|
|
44
|
+ if (result.Status == ResultStatus.PermissionError)
|
|
45
|
+ return "You don\'t have the permission to do this action";
|
|
46
|
+ if (result.Status == ResultStatus.InputError)
|
|
47
|
+ return "Invalid data was provided";
|
|
48
|
+ if (result.Status == ResultStatus.NotFound)
|
|
49
|
+ return "Ressource not found";
|
|
50
|
+
|
|
51
|
+ return "Internal error";
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ /// <summary>
|
|
55
|
+ /// Handle business manager results
|
|
56
|
+ /// </summary>
|
|
57
|
+ /// <param name="result">The result to handle</param>
|
|
58
|
+ /// <param name="request">The request to handle</param>
|
|
59
|
+ /// <returns>The data to return to the user</returns>
|
|
60
|
+ public static T Handle(OpResult<T> result, HttpRequestMessage request)
|
|
61
|
+ {
|
|
62
|
+ if (result.Status == ResultStatus.Success)
|
|
63
|
+ return result.Data;
|
|
64
|
+ var msg = request.CreateErrorResponse(ResultStatusToHttp(result.Status), OpResultToString(result));
|
|
65
|
+ throw new HttpResponseException(msg);
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+}
|