1: using System;
2: using System.Collections.Generic;
3: using System.Data;
4: using System.Linq;
5: using System.ComponentModel.DataAnnotations.Schema;
6: using System.Threading;
7: using System.Threading.Tasks;
8:
9: namespace Ia.Ngn.Cl.Model
10: {
11: ////////////////////////////////////////////////////////////////////////////
12:
13: /// <summary publish="true">
14: /// Transaction Entity Framework class for Optical Fiber Network (OFN) entity model.
15: /// </summary>
16: ///
17: /// <remarks>
18: /// Copyright © 2006-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
19: ///
20: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
21: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
22: ///
23: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25: ///
26: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
27: ///
28: /// Copyright notice: This notice may not be removed or altered from any source distribution.
29: /// </remarks>
30: public class Transaction
31: {
32: ////////////////////////////////////////////////////////////////////////////
33:
34: /// <summary>
35: ///
36: /// </summary>
37: public Transaction() { }
38:
39: /// <summary/>
40: public int Id { get; set; }
41:
42: /// <summary/>
43: public int TypeId { get; set; }
44:
45: /// <summary/>
46: public int StateId { get; set; }
47:
48: /// <summary/>
49: public int PriorityId { get; set; }
50:
51: /// <summary/>
52: public int RecipientId { get; set; }
53:
54: /// <summary/>
55: public int NumberOfTimesMessageSent { get; set; }
56:
57: /// <summary/>
58: public bool Closed { get; set; }
59:
60: /// <summary/>
61: public string Message { get; set; }
62:
63: /// <summary/>
64: public DateTime? MessageSent { get; set; }
65:
66: /// <summary/>
67: public string Response { get; set; }
68:
69: /// <summary/>
70: public DateTime? ResponseReceived { get; set; }
71:
72: /// <summary/>
73: public DateTime Created { get; set; }
74:
75: /// <summary/>
76: public DateTime Updated { get; set; }
77:
78: /// <summary/>
79: public System.Guid UserId { get; set; }
80:
81: ////////////////////////////////////////////////////////////////////////////
82: ////////////////////////////////////////////////////////////////////////////
83:
84: /// <summary>
85: ///
86: /// </summary>
87: public static bool Create(Transaction newItem, out int itemId)
88: {
89: bool b;
90:
91: b = false;
92:
93: using (var db = new Ia.Ngn.Cl.Model.Ngn())
94: {
95: newItem.Created = newItem.Updated = DateTime.UtcNow.AddHours(3);
96:
97: db.Transactions.Add(newItem);
98: db.SaveChanges();
99:
100: itemId = newItem.Id;
101:
102: b = true;
103: }
104:
105: return b;
106: }
107:
108: ////////////////////////////////////////////////////////////////////////////
109:
110: /// <summary>
111: ///
112: /// </summary>
113: public static Transaction Read(int id)
114: {
115: Transaction item;
116:
117: using (var db = new Ia.Ngn.Cl.Model.Ngn())
118: {
119: item = (from t in db.Transactions where t.Id == id select t).SingleOrDefault();
120: }
121:
122: return item;
123: }
124:
125: ////////////////////////////////////////////////////////////////////////////
126:
127: /// <summary>
128: ///
129: /// </summary>
130: public static List<Transaction> ReadList()
131: {
132: List<Transaction> itemList;
133:
134: using (var db = new Ia.Ngn.Cl.Model.Ngn())
135: {
136: itemList = (from t in db.Transactions select t).ToList();
137: }
138:
139: return itemList;
140: }
141:
142: ////////////////////////////////////////////////////////////////////////////
143:
144: /// <summary>
145: ///
146: /// </summary>
147: public static bool Update(Transaction updatedItem, out string result)
148: {
149: bool b;
150:
151: b = false;
152: result = string.Empty;
153:
154: using (var db = new Ia.Ngn.Cl.Model.Ngn())
155: {
156: updatedItem = (from t in db.Transactions where t.Id == updatedItem.Id select t).SingleOrDefault();
157:
158: updatedItem.Updated = DateTime.UtcNow.AddHours(3);
159:
160: db.Transactions.Attach(updatedItem);
161:
162: var v = db.Entry(updatedItem);
163: v.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
164: db.SaveChanges();
165:
166: b = true;
167: }
168:
169: return b;
170: }
171:
172: ////////////////////////////////////////////////////////////////////////////
173:
174: /// <summary>
175: ///
176: /// </summary>
177: public bool Update(Transaction transactionOnt)
178: {
179: // below: this will not update Id, Created
180: bool updated;
181:
182: updated = false;
183:
184: if (this.Closed != transactionOnt.Closed) { this.Closed = transactionOnt.Closed; updated = true; }
185: else if (this.Message != transactionOnt.Message) { this.Message = transactionOnt.Message; updated = true; }
186: else if (this.MessageSent != transactionOnt.MessageSent) { this.MessageSent = transactionOnt.MessageSent; updated = true; }
187: else if (this.NumberOfTimesMessageSent != transactionOnt.NumberOfTimesMessageSent) { this.NumberOfTimesMessageSent = transactionOnt.NumberOfTimesMessageSent; updated = true; }
188: else if (this.PriorityId != transactionOnt.PriorityId) { this.PriorityId = transactionOnt.PriorityId; updated = true; }
189: else if (this.RecipientId != transactionOnt.RecipientId) { this.RecipientId = transactionOnt.RecipientId; updated = true; }
190: else if (this.Response != transactionOnt.Response) { this.Response = transactionOnt.Response; updated = true; }
191: else if (this.ResponseReceived != transactionOnt.ResponseReceived) { this.ResponseReceived = transactionOnt.ResponseReceived; updated = true; }
192: else if (this.StateId != transactionOnt.StateId) { this.StateId = transactionOnt.StateId; updated = true; }
193: else if (this.TypeId != transactionOnt.TypeId) { this.TypeId = transactionOnt.TypeId; updated = true; }
194: else if (this.UserId != transactionOnt.UserId) { this.UserId = transactionOnt.UserId; updated = true; }
195:
196: if (updated) this.Updated = DateTime.UtcNow.AddHours(3);
197:
198: return updated;
199: }
200:
201: ////////////////////////////////////////////////////////////////////////////
202:
203: /// <summary>
204: ///
205: /// </summary>
206: public static bool Delete(int id, out string result)
207: {
208: bool b;
209:
210: b = false;
211: result = string.Empty;
212:
213: using (var db = new Ia.Ngn.Cl.Model.Ngn())
214: {
215: var v = (from t in db.Transactions where t.Id == id select t).FirstOrDefault();
216:
217: db.Transactions.Remove(v);
218: db.SaveChanges();
219:
220: b = true;
221: }
222:
223: return b;
224: }
225:
226: ////////////////////////////////////////////////////////////////////////////
227: ////////////////////////////////////////////////////////////////////////////
228: }
229:
230: ////////////////////////////////////////////////////////////////////////////
231: ////////////////////////////////////////////////////////////////////////////
232: }