Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SetContractState.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
6using Waher.Events;
14using Waher.Security;
16
18{
20 {
21 public SetContractState()
22 : base("/SetContractState")
23 {
24 }
25
26 public override bool HandlesSubPaths => false;
27 public override bool UserSessions => true;
28 public bool AllowsPOST => true;
29
30 public async Task POST(HttpRequest Request, HttpResponse Response)
31 {
32 IUser User = Gateway.AssertUserAuthenticated(Request, "Admin.Notarius.Contracts");
33
34 if (!Request.HasData)
35 throw new BadRequestException();
36
37 object Obj = await Request.DecodeDataAsync();
38 if (!(Obj is Dictionary<string, object> Form))
39 throw new BadRequestException();
40
41 if (!Form.TryGetValue("contractId", out Obj) || !(Obj is string ContractId) ||
42 !Form.TryGetValue("state", out Obj) || !(Obj is string StateStr) ||
43 !Enum.TryParse(StateStr, out ContractState State))
44 {
45 throw new BadRequestException();
46 }
47
48 Dictionary<string, object> Result = await SetState(ContractId, State, User);
49
50 Response.StatusCode = 200;
51 Response.ContentType = JsonCodec.DefaultContentType;
52
53 await Response.Write(JSON.Encode(Result, false));
54 }
55
56 internal static async Task<Dictionary<string, object>> SetState(string ContractId, ContractState State, IUser User)
57 {
58 using (Semaphore Semaphore = await Semaphores.BeginWrite("iotsc:" + ContractId.ToLower()))
59 {
60 Contract Contract = await Database.FindFirstIgnoreRest<Contract>(new FilterFieldEqualTo("ContractId", ContractId))
61 ?? throw new NotFoundException("Contract not found.");
62
63 if (Contract.State == State)
64 {
65 return new Dictionary<string, object>()
66 {
67 { "message", null }
68 };
69 }
70
71 DateTime Now = DateTime.Now;
72 string Message;
73
74 Contract.State = State;
75 Contract.Sign(XmppServerModule.Legal);
76
77 if (State == ContractState.Deleted)
78 {
80 Message = "Contract deleted.";
81 }
82 else
83 {
85 Message = "Contract state changed to " + State.ToString() + ".";
86 }
87
88 await XmppServerModule.Legal.SendContractUpdatedEvent(Contract, false);
89
90 Log.Informational(Message, ContractId, User.UserName,
91 new KeyValuePair<string, object>("State", State));
92
93 return new Dictionary<string, object>()
94 {
95 { "date", Now.ToShortDateString() },
96 { "time", Now.ToLongTimeString() },
97 { "type", EventType.Informational },
98 { "object", ContractId },
99 { "actor", User.UserName },
100 { "message", Message },
101 { "signDate", Contract.ServerSignature.Timestamp.ToShortDateString() },
102 { "signTime", Contract.ServerSignature.Timestamp.ToLongTimeString() },
103 { "signature", Convert.ToBase64String(Contract.ServerSignature.DigitalSignature) }
104 };
105 }
106 }
107 }
108}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
const string DefaultContentType
application/json
Definition: JsonCodec.cs:19
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:334
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3041
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:717
This filter selects objects that have a named field equal to a given value.
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:16
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:90
Current date and time.
Definition: Now.cs:12
Now()
Current date and time.
Definition: Now.cs:16
bool AllowsPOST
If the POST method is allowed.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
POST Interface for HTTP resources.
Basic interface for a user.
Definition: IUser.cs:7
string UserName
User Name.
Definition: IUser.cs:12