diff options
Diffstat (limited to 'ApartmentManager/HousingWebAPI/Controllers/UsersController.cs')
-rw-r--r-- | ApartmentManager/HousingWebAPI/Controllers/UsersController.cs | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/ApartmentManager/HousingWebAPI/Controllers/UsersController.cs b/ApartmentManager/HousingWebAPI/Controllers/UsersController.cs new file mode 100644 index 0000000..dbbc31c --- /dev/null +++ b/ApartmentManager/HousingWebAPI/Controllers/UsersController.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Entity; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; +using System.Web.Http.Description; +using HousingWebAPI.Models; + +namespace HousingWebAPI.Controllers +{ + public class UsersController : ApiController + { + private ApartmentsDataContext db = new ApartmentsDataContext(); + + // GET: api/UsersVartotojas + public IQueryable<Users> GetUsers() + { + return db.Users; + } + + // GET: api/Users/5 + [ResponseType(typeof(Users))] + public IHttpActionResult GetUsers(int id) + { + Users users = db.Users.Find(id); + if (users == null) + { + return NotFound(); + } + + return Ok(users); + } + + // PUT: api/Users/5 + [ResponseType(typeof(void))] + public IHttpActionResult PutUsers(int id, Users users) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + if (id != users.ResidentNumber) + { + return BadRequest(); + } + + db.Entry(users).State = EntityState.Modified; + + try + { + db.SaveChanges(); + } + catch (DbUpdateConcurrencyException) + { + if (!UsersExists(id)) + { + return NotFound(); + } + else + { + throw; + } + } + + return StatusCode(HttpStatusCode.NoContent); + } + + // POST: api/Users + [ResponseType(typeof(Users))] + public IHttpActionResult PostUsers(Users users) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + db.Users.Add(users); + + try + { + db.SaveChanges(); + } + catch (DbUpdateException) + { + if (UsersExists(users.ResidentNumber)) + { + return Conflict(); + } + else + { + throw; + } + } + + return CreatedAtRoute("DefaultApi", new { id = users.ResidentNumber }, users); + } + + // DELETE: api/Users/5 + [ResponseType(typeof(Users))] + public IHttpActionResult DeleteUsers(int id) + { + Users users = db.Users.Find(id); + if (users == null) + { + return NotFound(); + } + + db.Users.Remove(users); + db.SaveChanges(); + + return Ok(users); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + db.Dispose(); + } + base.Dispose(disposing); + } + + private bool UsersExists(int id) + { + return db.Users.Count(e => e.ResidentNumber == id) > 0; + } + } +}
\ No newline at end of file |