Bläddra i källkod

Add vendors/nextinternalref API.

Andrew Klopper 8 år sedan
förälder
incheckning
cd148601f4

+ 51 - 0
BulkPrintingAPI/Controllers/VendorsController.cs

@@ -0,0 +1,51 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Data.SqlClient;
4
+using System.Linq;
5
+using System.Threading.Tasks;
6
+using Microsoft.AspNetCore.Http;
7
+using Microsoft.AspNetCore.Mvc;
8
+using Microsoft.EntityFrameworkCore;
9
+using MAX.Models;
10
+
11
+namespace BulkPrintingAPI.Controllers
12
+{
13
+    [Produces("application/json")]
14
+    [Route("api/[controller]")]
15
+    public class VendorsController : Controller
16
+    {
17
+        private readonly MAXContext _context;
18
+
19
+        public VendorsController(MAXContext context)
20
+        {
21
+            _context = context;
22
+        }
23
+
24
+        [HttpGet("nextinternalref")]
25
+        public async Task<IActionResult> GetNextInternalReferenceAsync()
26
+        {
27
+            var credentials = await Utils.GetLoginCredentialsFromRequestAsync(HttpContext, _context);
28
+
29
+            using (var connection = _context.Database.GetDbConnection())
30
+            {
31
+                await connection.OpenAsync();
32
+                using (var command = connection.CreateCommand())
33
+                {
34
+                    command.CommandText =
35
+                        "DECLARE @Ret table (InternalReferenceCounter int NOT NULL);" +
36
+                        "UPDATE Vendors SET InternalReferenceCounter = InternalReferenceCounter + 1 OUTPUT inserted.InternalReferenceCounter INTO @Ret WHERE Id = @VendorId;" +
37
+                        "SELECT InternalReferenceCounter FROM @Ret;";
38
+
39
+                    var param = command.CreateParameter();
40
+                    param.ParameterName = "@VendorId";
41
+                    param.Value = credentials.Vendor.Id;
42
+                    command.Parameters.Add(param);
43
+
44
+                    var ret = await command.ExecuteScalarAsync();
45
+
46
+                    return Ok(new { internalReference = string.Format("{0}-{1}", credentials.Vendor.Id, ret) });
47
+                }
48
+            }
49
+        }
50
+    }
51
+}

+ 302 - 0
BulkPrintingAPI/Migrations/20180111115544_AddInternalReferenceCounter.Designer.cs

@@ -0,0 +1,302 @@
1
+using System;
2
+using Microsoft.EntityFrameworkCore;
3
+using Microsoft.EntityFrameworkCore.Infrastructure;
4
+using Microsoft.EntityFrameworkCore.Metadata;
5
+using Microsoft.EntityFrameworkCore.Migrations;
6
+using MAX.Models;
7
+
8
+namespace BulkPrintingAPI.Migrations
9
+{
10
+    [DbContext(typeof(MAXContext))]
11
+    [Migration("20180111115544_AddInternalReferenceCounter")]
12
+    partial class AddInternalReferenceCounter
13
+    {
14
+        protected override void BuildTargetModel(ModelBuilder modelBuilder)
15
+        {
16
+            modelBuilder
17
+                .HasAnnotation("ProductVersion", "1.1.2")
18
+                .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
19
+
20
+            modelBuilder.Entity("MAX.Models.Account", b =>
21
+                {
22
+                    b.Property<int>("Id");
23
+
24
+                    b.Property<decimal>("Balance");
25
+
26
+                    b.Property<string>("Name")
27
+                        .IsRequired()
28
+                        .HasMaxLength(50);
29
+
30
+                    b.Property<string>("Reference")
31
+                        .IsRequired()
32
+                        .HasMaxLength(50);
33
+
34
+                    b.Property<int>("Status");
35
+
36
+                    b.Property<int>("WarehouseId");
37
+
38
+                    b.HasKey("Id");
39
+
40
+                    b.HasIndex("WarehouseId");
41
+
42
+                    b.ToTable("Accounts");
43
+                });
44
+
45
+            modelBuilder.Entity("MAX.Models.Batch", b =>
46
+                {
47
+                    b.Property<int>("Id");
48
+
49
+                    b.Property<int>("AccountId");
50
+
51
+                    b.Property<decimal>("Cost");
52
+
53
+                    b.Property<int>("DeliveredQuantity");
54
+
55
+                    b.Property<decimal>("DiscountPercentage");
56
+
57
+                    b.Property<decimal>("FaceValue");
58
+
59
+                    b.Property<string>("InternalReference")
60
+                        .HasMaxLength(32);
61
+
62
+                    b.Property<int>("NetworkId");
63
+
64
+                    b.Property<string>("NetworkName")
65
+                        .IsRequired()
66
+                        .HasMaxLength(20);
67
+
68
+                    b.Property<DateTimeOffset>("OrderDate");
69
+
70
+                    b.Property<Guid?>("OrderGuid");
71
+
72
+                    b.Property<string>("OrderReference")
73
+                        .IsRequired()
74
+                        .HasMaxLength(20);
75
+
76
+                    b.Property<int>("OrderedById");
77
+
78
+                    b.Property<string>("ProductDescription")
79
+                        .IsRequired()
80
+                        .HasMaxLength(50);
81
+
82
+                    b.Property<int>("ProductId");
83
+
84
+                    b.Property<bool>("ReadyForDownload");
85
+
86
+                    b.Property<int>("RequestedQuantity");
87
+
88
+                    b.Property<int>("VendorId");
89
+
90
+                    b.Property<int>("VoucherType");
91
+
92
+                    b.HasKey("Id");
93
+
94
+                    b.HasIndex("AccountId");
95
+
96
+                    b.HasIndex("OrderGuid")
97
+                        .IsUnique();
98
+
99
+                    b.HasIndex("ReadyForDownload", "OrderDate");
100
+
101
+                    b.ToTable("Batches");
102
+                });
103
+
104
+            modelBuilder.Entity("MAX.Models.User", b =>
105
+                {
106
+                    b.Property<int>("Id");
107
+
108
+                    b.Property<int>("AccountId");
109
+
110
+                    b.Property<bool>("BulkExport");
111
+
112
+                    b.Property<decimal>("BulkExportMaxValue");
113
+
114
+                    b.Property<bool>("BulkOrder");
115
+
116
+                    b.Property<decimal>("BulkOrderMaxValue");
117
+
118
+                    b.Property<bool>("BulkReExport");
119
+
120
+                    b.Property<bool>("BulkViewPins");
121
+
122
+                    b.Property<bool>("CanPrintOffline");
123
+
124
+                    b.Property<bool>("CanPrintOnline");
125
+
126
+                    b.Property<bool>("CanReprintOffline");
127
+
128
+                    b.Property<bool>("CanReprintOnline");
129
+
130
+                    b.Property<bool>("Enabled");
131
+
132
+                    b.Property<string>("FirstName")
133
+                        .IsRequired()
134
+                        .HasMaxLength(50);
135
+
136
+                    b.Property<DateTimeOffset>("LastLogin");
137
+
138
+                    b.Property<int>("Level");
139
+
140
+                    b.Property<decimal>("OfflinePrintValue");
141
+
142
+                    b.Property<decimal>("OfflineReprintValue");
143
+
144
+                    b.Property<decimal>("OnlinePrintValue");
145
+
146
+                    b.Property<decimal>("OnlineReprintValue");
147
+
148
+                    b.Property<string>("Surname")
149
+                        .IsRequired()
150
+                        .HasMaxLength(50);
151
+
152
+                    b.Property<int>("System");
153
+
154
+                    b.Property<string>("Username")
155
+                        .IsRequired()
156
+                        .HasMaxLength(50);
157
+
158
+                    b.HasKey("Id");
159
+
160
+                    b.HasIndex("AccountId");
161
+
162
+                    b.ToTable("Users");
163
+                });
164
+
165
+            modelBuilder.Entity("MAX.Models.Vendor", b =>
166
+                {
167
+                    b.Property<int>("Id");
168
+
169
+                    b.Property<int>("AccountId");
170
+
171
+                    b.Property<byte[]>("EncryptedDatabasePassword")
172
+                        .IsRequired()
173
+                        .HasMaxLength(32);
174
+
175
+                    b.Property<byte[]>("EncryptedVoucherKey")
176
+                        .IsRequired()
177
+                        .HasMaxLength(32);
178
+
179
+                    b.Property<int>("InternalReferenceCounter");
180
+
181
+                    b.Property<int?>("LastVendorEventRemoteId");
182
+
183
+                    b.Property<string>("SerialNumber")
184
+                        .IsRequired()
185
+                        .HasMaxLength(50);
186
+
187
+                    b.HasKey("Id");
188
+
189
+                    b.HasIndex("AccountId");
190
+
191
+                    b.ToTable("Vendors");
192
+                });
193
+
194
+            modelBuilder.Entity("MAX.Models.VendorEvent", b =>
195
+                {
196
+                    b.Property<int>("Id")
197
+                        .ValueGeneratedOnAdd();
198
+
199
+                    b.Property<DateTimeOffset>("EventDate");
200
+
201
+                    b.Property<int>("EventType");
202
+
203
+                    b.Property<int?>("RemoteId");
204
+
205
+                    b.Property<bool>("Retry");
206
+
207
+                    b.Property<int>("UserId");
208
+
209
+                    b.Property<int>("VendorId");
210
+
211
+                    b.Property<int?>("VoucherId");
212
+
213
+                    b.HasKey("Id");
214
+
215
+                    b.HasIndex("VendorId", "RemoteId")
216
+                        .IsUnique();
217
+
218
+                    b.ToTable("VendorEvents");
219
+                });
220
+
221
+            modelBuilder.Entity("MAX.Models.Voucher", b =>
222
+                {
223
+                    b.Property<int>("Id");
224
+
225
+                    b.Property<int>("BatchId");
226
+
227
+                    b.Property<string>("EncryptedPIN")
228
+                        .IsRequired()
229
+                        .HasMaxLength(50);
230
+
231
+                    b.Property<DateTime>("ExpiryDate")
232
+                        .HasColumnType("Date");
233
+
234
+                    b.Property<int>("SequenceNumber");
235
+
236
+                    b.Property<string>("Serial")
237
+                        .IsRequired()
238
+                        .HasMaxLength(50);
239
+
240
+                    b.HasKey("Id");
241
+
242
+                    b.HasIndex("BatchId", "SequenceNumber")
243
+                        .IsUnique();
244
+
245
+                    b.ToTable("Vouchers");
246
+                });
247
+
248
+            modelBuilder.Entity("MAX.Models.Warehouse", b =>
249
+                {
250
+                    b.Property<int>("Id");
251
+
252
+                    b.Property<string>("Name")
253
+                        .IsRequired()
254
+                        .HasMaxLength(50);
255
+
256
+                    b.HasKey("Id");
257
+
258
+                    b.ToTable("Warehouses");
259
+                });
260
+
261
+            modelBuilder.Entity("MAX.Models.Account", b =>
262
+                {
263
+                    b.HasOne("MAX.Models.Warehouse", "Warehouse")
264
+                        .WithMany("Accounts")
265
+                        .HasForeignKey("WarehouseId")
266
+                        .OnDelete(DeleteBehavior.Cascade);
267
+                });
268
+
269
+            modelBuilder.Entity("MAX.Models.Batch", b =>
270
+                {
271
+                    b.HasOne("MAX.Models.Account", "Account")
272
+                        .WithMany()
273
+                        .HasForeignKey("AccountId")
274
+                        .OnDelete(DeleteBehavior.Cascade);
275
+                });
276
+
277
+            modelBuilder.Entity("MAX.Models.User", b =>
278
+                {
279
+                    b.HasOne("MAX.Models.Account", "Account")
280
+                        .WithMany("Users")
281
+                        .HasForeignKey("AccountId")
282
+                        .OnDelete(DeleteBehavior.Cascade);
283
+                });
284
+
285
+            modelBuilder.Entity("MAX.Models.Vendor", b =>
286
+                {
287
+                    b.HasOne("MAX.Models.Account", "Account")
288
+                        .WithMany("Vendors")
289
+                        .HasForeignKey("AccountId")
290
+                        .OnDelete(DeleteBehavior.Cascade);
291
+                });
292
+
293
+            modelBuilder.Entity("MAX.Models.Voucher", b =>
294
+                {
295
+                    b.HasOne("MAX.Models.Batch", "Batch")
296
+                        .WithMany("Vouchers")
297
+                        .HasForeignKey("BatchId")
298
+                        .OnDelete(DeleteBehavior.Cascade);
299
+                });
300
+        }
301
+    }
302
+}

+ 25 - 0
BulkPrintingAPI/Migrations/20180111115544_AddInternalReferenceCounter.cs

@@ -0,0 +1,25 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using Microsoft.EntityFrameworkCore.Migrations;
4
+
5
+namespace BulkPrintingAPI.Migrations
6
+{
7
+    public partial class AddInternalReferenceCounter : Migration
8
+    {
9
+        protected override void Up(MigrationBuilder migrationBuilder)
10
+        {
11
+            migrationBuilder.AddColumn<int>(
12
+                name: "InternalReferenceCounter",
13
+                table: "Vendors",
14
+                nullable: false,
15
+                defaultValue: 0);
16
+        }
17
+
18
+        protected override void Down(MigrationBuilder migrationBuilder)
19
+        {
20
+            migrationBuilder.DropColumn(
21
+                name: "InternalReferenceCounter",
22
+                table: "Vendors");
23
+        }
24
+    }
25
+}

+ 2 - 0
BulkPrintingAPI/Migrations/MAXContextModelSnapshot.cs

@@ -175,6 +175,8 @@ namespace BulkPrintingAPI.Migrations
175 175
                         .IsRequired()
176 176
                         .HasMaxLength(32);
177 177
 
178
+                    b.Property<int>("InternalReferenceCounter");
179
+
178 180
                     b.Property<int?>("LastVendorEventRemoteId");
179 181
 
180 182
                     b.Property<string>("SerialNumber")

+ 3 - 0
MAXData/Models/Vendor.cs

@@ -25,6 +25,9 @@ namespace MAX.Models
25 25
         [Required, MaxLength(32)]
26 26
         public byte[] EncryptedVoucherKey { get; set; }
27 27
 
28
+        [IgnoreDataMember]
29
+        public int InternalReferenceCounter { get; set; }
30
+
28 31
         public int? LastVendorEventRemoteId { get; set; }
29 32
     }
30 33
 }