Skip to content

Commit 0f0d5d1

Browse files
committed
pull success
2 parents bbb0243 + 205acbc commit 0f0d5d1

File tree

5 files changed

+392
-0
lines changed

5 files changed

+392
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
using System.Data.Entity.Infrastructure;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Web;
8+
using System.Web.Http;
9+
using System.Web.Http.Description;
10+
using Zenergy.Models;
11+
using System.Web.Http.Results;
12+
13+
using Zenergy.Services;
14+
15+
namespace Zenergy.Controllers.ApiControllers
16+
{
17+
public class eventsController : ApiController
18+
{
19+
private ZenergyContext db = new ZenergyContext();
20+
21+
/// <summary>
22+
/// Register user to the event.
23+
/// </summary>
24+
/// <param name="model"></param>
25+
/// <returns></returns>
26+
// POST: api/events/PostRegisterToEvent
27+
[HttpPost]
28+
[ResponseType(typeof(EventRegistrationModel))]
29+
public async Task<IHttpActionResult> PostRegisterToEvent(EventRegistrationModel model)
30+
{
31+
if (EventExists(model.eventId))
32+
{
33+
if (!UserAlreadyRegisteredToEvent(model.userId, model.eventId))
34+
{
35+
var myUser = await db.user.Where(u => u.userId == model.userId).FirstAsync();
36+
var myEvent = await db.@event.Where(e => e.eventId == model.eventId).FirstAsync();
37+
38+
try
39+
{
40+
myEvent.user.Add(myUser);
41+
db.Entry(myEvent).State = EntityState.Modified;
42+
await db.SaveChangesAsync();
43+
return CreatedAtRoute("DefaultApi", new { id = model.userId }, model);
44+
}
45+
catch (DbUpdateException)
46+
{
47+
return BadRequest("Sorry an error occured. Please try again.");
48+
}
49+
}
50+
else return BadRequest("You are already registered to this event");
51+
}
52+
else return BadRequest("This event does not exist.");
53+
54+
}
55+
56+
57+
/// <summary>
58+
/// Unsubscribe a user to an event.
59+
/// </summary>
60+
/// <param name="eventId"></param>
61+
/// <param name="userId"></param>
62+
/// <returns></returns>
63+
// DELETE: api/events/DeleteRegistration
64+
[HttpDelete]
65+
[ResponseType(typeof(EventRegistrationModel))]
66+
public async Task<IHttpActionResult> DeleteRegistration(int eventId, int userId)
67+
{
68+
if (EventExists(eventId))
69+
{
70+
if(UserAlreadyRegisteredToEvent(userId, eventId))
71+
{
72+
var myEvent = await db.@event.FindAsync(eventId);
73+
var myUser = await db.user.FindAsync(userId);
74+
myEvent.user.Remove(myUser);
75+
await db.SaveChangesAsync();
76+
return Ok(new EventRegistrationModel() { eventId = eventId, userId = userId });
77+
}
78+
return BadRequest("You are not registered to this event.");
79+
}
80+
return BadRequest("The event does not exist.");
81+
}
82+
83+
84+
protected override void Dispose(bool disposing)
85+
{
86+
if (disposing)
87+
{
88+
db.Dispose();
89+
}
90+
base.Dispose(disposing);
91+
}
92+
93+
public bool EventExists(int eventId)
94+
{
95+
return db.@event.Where(e => e.eventId == eventId).Any();
96+
}
97+
98+
public bool UserAlreadyRegisteredToEvent(int userId, int eventId)
99+
{
100+
return db.@event.Where(e => e.eventId == eventId).First().user.Where(u => u.userId == userId).Any();
101+
}
102+
103+
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Entity;
5+
using System.Data.Entity.Infrastructure;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Threading.Tasks;
10+
using System.Web.Http;
11+
using System.Web.Http.Description;
12+
using Zenergy.Models;
13+
using Zenergy.Services;
14+
15+
namespace Zenergy.Controllers.ApiControllers
16+
{
17+
public class ponctualEventsController : ApiController
18+
{
19+
private ZenergyContext db = new ZenergyContext();
20+
21+
// GET: api/ponctualEvents
22+
public IQueryable<ponctualEvent> GetponctualEvent()
23+
{
24+
return db.ponctualEvent;
25+
}
26+
27+
// GET: api/ponctualEvents/5
28+
[ResponseType(typeof(ponctualEvent))]
29+
public async Task<IHttpActionResult> GetponctualEvent(int id)
30+
{
31+
ponctualEvent ponctualEvent = await db.ponctualEvent.FindAsync(id);
32+
if (ponctualEvent == null)
33+
{
34+
return NotFound();
35+
}
36+
37+
return Ok(ponctualEvent);
38+
}
39+
40+
// PUT: api/ponctualEvents/5
41+
[ResponseType(typeof(void))]
42+
public async Task<IHttpActionResult> PutponctualEvent(int id, ponctualEvent ponctualEvent)
43+
{
44+
if (!ModelState.IsValid)
45+
{
46+
return BadRequest(ModelState);
47+
}
48+
49+
if (id != ponctualEvent.eventId)
50+
{
51+
return BadRequest();
52+
}
53+
54+
db.Entry(ponctualEvent).State = EntityState.Modified;
55+
56+
try
57+
{
58+
await db.SaveChangesAsync();
59+
}
60+
catch (DbUpdateConcurrencyException)
61+
{
62+
if (!ponctualEventExists(id))
63+
{
64+
return NotFound();
65+
}
66+
else
67+
{
68+
throw;
69+
}
70+
}
71+
72+
return StatusCode(HttpStatusCode.NoContent);
73+
}
74+
75+
// POST: api/ponctualEvents
76+
[ResponseType(typeof(ponctualEvent))]
77+
public async Task<IHttpActionResult> PostponctualEvent(ponctualEvent ponctualEvent)
78+
{
79+
if (!ModelState.IsValid)
80+
{
81+
return BadRequest(ModelState);
82+
}
83+
84+
db.ponctualEvent.Add(ponctualEvent);
85+
86+
try
87+
{
88+
await db.SaveChangesAsync();
89+
}
90+
catch (DbUpdateException)
91+
{
92+
if (ponctualEventExists(ponctualEvent.eventId))
93+
{
94+
return Conflict();
95+
}
96+
else
97+
{
98+
throw;
99+
}
100+
}
101+
102+
return CreatedAtRoute("DefaultApi", new { id = ponctualEvent.eventId }, ponctualEvent);
103+
}
104+
105+
// DELETE: api/ponctualEvents/5
106+
[ResponseType(typeof(ponctualEvent))]
107+
public async Task<IHttpActionResult> DeleteponctualEvent(int id)
108+
{
109+
ponctualEvent ponctualEvent = await db.ponctualEvent.FindAsync(id);
110+
if (ponctualEvent == null)
111+
{
112+
return NotFound();
113+
}
114+
115+
db.ponctualEvent.Remove(ponctualEvent);
116+
await db.SaveChangesAsync();
117+
118+
return Ok(ponctualEvent);
119+
}
120+
121+
protected override void Dispose(bool disposing)
122+
{
123+
if (disposing)
124+
{
125+
db.Dispose();
126+
}
127+
base.Dispose(disposing);
128+
}
129+
130+
private bool ponctualEventExists(int id)
131+
{
132+
return db.ponctualEvent.Count(e => e.eventId == id) > 0;
133+
}
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Entity;
5+
using System.Data.Entity.Infrastructure;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Threading.Tasks;
10+
using System.Web.Http;
11+
using System.Web.Http.Description;
12+
using Zenergy.Models;
13+
14+
namespace Zenergy.Controllers.ApiControllers
15+
{
16+
public class regularEventsController : ApiController
17+
{
18+
private ZenergyContext db = new ZenergyContext();
19+
20+
// GET: api/regularEvents
21+
public IQueryable<regularEvent> GetregularEvent()
22+
{
23+
return db.regularEvent;
24+
}
25+
26+
// GET: api/regularEvents/5
27+
[ResponseType(typeof(regularEvent))]
28+
public async Task<IHttpActionResult> GetregularEvent(int id)
29+
{
30+
regularEvent regularEvent = await db.regularEvent.FindAsync(id);
31+
if (regularEvent == null)
32+
{
33+
return NotFound();
34+
}
35+
36+
return Ok(regularEvent);
37+
}
38+
39+
// PUT: api/regularEvents/5
40+
[ResponseType(typeof(void))]
41+
public async Task<IHttpActionResult> PutregularEvent(int id, regularEvent regularEvent)
42+
{
43+
if (!ModelState.IsValid)
44+
{
45+
return BadRequest(ModelState);
46+
}
47+
48+
if (id != regularEvent.eventId)
49+
{
50+
return BadRequest();
51+
}
52+
53+
db.Entry(regularEvent).State = EntityState.Modified;
54+
55+
try
56+
{
57+
await db.SaveChangesAsync();
58+
}
59+
catch (DbUpdateConcurrencyException)
60+
{
61+
if (!regularEventExists(id))
62+
{
63+
return NotFound();
64+
}
65+
else
66+
{
67+
throw;
68+
}
69+
}
70+
71+
return StatusCode(HttpStatusCode.NoContent);
72+
}
73+
74+
// POST: api/regularEvents
75+
[ResponseType(typeof(regularEvent))]
76+
public async Task<IHttpActionResult> PostregularEvent(regularEvent regularEvent)
77+
{
78+
if (!ModelState.IsValid)
79+
{
80+
return BadRequest(ModelState);
81+
}
82+
83+
db.regularEvent.Add(regularEvent);
84+
85+
try
86+
{
87+
await db.SaveChangesAsync();
88+
}
89+
catch (DbUpdateException)
90+
{
91+
if (regularEventExists(regularEvent.eventId))
92+
{
93+
return Conflict();
94+
}
95+
else
96+
{
97+
throw;
98+
}
99+
}
100+
101+
return CreatedAtRoute("DefaultApi", new { id = regularEvent.eventId }, regularEvent);
102+
}
103+
104+
// DELETE: api/regularEvents/5
105+
[ResponseType(typeof(regularEvent))]
106+
public async Task<IHttpActionResult> DeleteregularEvent(int id)
107+
{
108+
regularEvent regularEvent = await db.regularEvent.FindAsync(id);
109+
if (regularEvent == null)
110+
{
111+
return NotFound();
112+
}
113+
114+
db.regularEvent.Remove(regularEvent);
115+
await db.SaveChangesAsync();
116+
117+
return Ok(regularEvent);
118+
}
119+
120+
protected override void Dispose(bool disposing)
121+
{
122+
if (disposing)
123+
{
124+
db.Dispose();
125+
}
126+
base.Dispose(disposing);
127+
}
128+
129+
private bool regularEventExists(int id)
130+
{
131+
return db.regularEvent.Count(e => e.eventId == id) > 0;
132+
}
133+
}
134+
}

Diff for: Zenergy/Zenergy/Models/EventRegistrationModel.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace Zenergy.Models
7+
{
8+
public class EventRegistrationModel
9+
{
10+
public int eventId { get; set; }
11+
public int userId { get; set; }
12+
}
13+
}

0 commit comments

Comments
 (0)