1
+ using Newtonsoft . Json ;
2
+ using System ;
3
+ using System . Collections . Generic ;
4
+ using System . Data ;
5
+ using System . Data . Entity ;
6
+ using System . Data . Entity . Infrastructure ;
7
+ using System . Linq ;
8
+ using System . Net ;
9
+ using System . Net . Http ;
10
+ using System . Threading . Tasks ;
11
+ using System . Web ;
12
+ using System . Web . Http ;
13
+ using System . Web . Http . Description ;
14
+ using Zenergy . Models ;
15
+
16
+ namespace Zenergy . Controllers . ApiControllers
17
+ {
18
+ public class accessoriesController : ApiController
19
+ {
20
+ private ZenergyContext db = new ZenergyContext ( ) ;
21
+
22
+ // GET: api/accessories
23
+ public IQueryable < accessory > Getaccessory ( )
24
+ {
25
+ return db . accessory ;
26
+ }
27
+
28
+ // GET: api/accessories/5
29
+ [ ResponseType ( typeof ( accessory ) ) ]
30
+ public async Task < IHttpActionResult > Getaccessory ( int id )
31
+ {
32
+ accessory accessory = await db . accessory . FindAsync ( id ) ;
33
+ if ( accessory == null )
34
+ {
35
+ return NotFound ( ) ;
36
+ }
37
+
38
+ return Ok ( accessory ) ;
39
+ }
40
+
41
+ // PUT: api/accessories/5
42
+ [ ResponseType ( typeof ( void ) ) ]
43
+ public async Task < IHttpActionResult > Putaccessory ( int id , accessory accessory )
44
+ {
45
+ if ( ! ModelState . IsValid )
46
+ {
47
+ return BadRequest ( ModelState ) ;
48
+ }
49
+
50
+ if ( id != accessory . accessoryId )
51
+ {
52
+ return BadRequest ( ) ;
53
+ }
54
+
55
+ db . Entry ( accessory ) . State = EntityState . Modified ;
56
+
57
+ try
58
+ {
59
+ await db . SaveChangesAsync ( ) ;
60
+ }
61
+ catch ( DbUpdateConcurrencyException )
62
+ {
63
+ if ( ! accessoryExists ( id ) )
64
+ {
65
+ return NotFound ( ) ;
66
+ }
67
+ else
68
+ {
69
+ throw ;
70
+ }
71
+ }
72
+
73
+ return StatusCode ( HttpStatusCode . NoContent ) ;
74
+ }
75
+
76
+ // POST: api/accessories
77
+ [ ResponseType ( typeof ( accessory ) ) ]
78
+ public async Task < IHttpActionResult > Postaccessory ( )
79
+ {
80
+ var context = HttpContext . Current ;
81
+ var data = HttpContext . Current . Request . Form ;
82
+ string postData = new System . IO . StreamReader ( context . Request . InputStream ) . ReadToEnd ( ) ;
83
+ accessory accessory = JsonConvert . DeserializeObject < accessory > ( postData ) ;
84
+ db . accessory . Add ( accessory ) ;
85
+ await db . SaveChangesAsync ( ) ;
86
+
87
+ return CreatedAtRoute ( "DefaultApi" , new { id = accessory . accessoryId } , accessory ) ;
88
+ }
89
+
90
+ // DELETE: api/accessories/5
91
+ [ ResponseType ( typeof ( accessory ) ) ]
92
+ public async Task < IHttpActionResult > Deleteaccessory ( int id )
93
+ {
94
+ accessory accessory = await db . accessory . FindAsync ( id ) ;
95
+ if ( accessory == null )
96
+ {
97
+ return NotFound ( ) ;
98
+ }
99
+
100
+ db . accessory . Remove ( accessory ) ;
101
+ await db . SaveChangesAsync ( ) ;
102
+
103
+ return Ok ( accessory ) ;
104
+ }
105
+
106
+ protected override void Dispose ( bool disposing )
107
+ {
108
+ if ( disposing )
109
+ {
110
+ db . Dispose ( ) ;
111
+ }
112
+ base . Dispose ( disposing ) ;
113
+ }
114
+
115
+ private bool accessoryExists ( int id )
116
+ {
117
+ return db . accessory . Count ( e => e . accessoryId == id ) > 0 ;
118
+ }
119
+ }
120
+ }
0 commit comments