/
lirikkrd
/
API
Обзор
Документация
Войти
/
lirikkrd
/
API
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Controllers/HotelsController.cs
108 строк
3 KB
kiril
Добавьте файлы проекта.
12 ноя 2025, 22:20
12 ноя 2025, 22:20
7811891
Код
Авторство
О чём код?
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 ToursWebApi.Entities; using ToursWebApi.Models; namespace ToursWebApi.Controllers { public class HotelsController : ApiController { private ToursBaseEntities db = new ToursBaseEntities(); // GET: api/Hotels/5 [ResponseType(typeof(List<ResponseHotel>))] public IHttpActionResult GetHotels() { return Ok(db.Hotels.ToList().ConvertAll(p=> new ResponseHotel(p))); } // PUT: api/Hotels/5 [ResponseType(typeof(void))] public IHttpActionResult PutHotel(int id, Hotel hotel) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != hotel.Id) { return BadRequest(); } db.Entry(hotel).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!HotelExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/Hotels [ResponseType(typeof(Hotel))] public IHttpActionResult PostHotel(Hotel hotel) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Hotels.Add(hotel); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = hotel.Id }, hotel); } // DELETE: api/Hotels/5 [ResponseType(typeof(Hotel))] public IHttpActionResult DeleteHotel(int id) { Hotel hotel = db.Hotels.Find(id); if (hotel == null) { return NotFound(); } db.Hotels.Remove(hotel); db.SaveChanges(); return Ok(hotel); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool HotelExists(int id) { return db.Hotels.Count(e => e.Id == id) > 0; } } }