/
kxekxel
/
lab
Обзор
Документация
Войти
/
kxekxel
/
lab
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Controllers/HotelCommentsController.cs
140 строк
4 KB
kxekxel
upload files
27 ноя 2025, 12:39
27 ноя 2025, 12:39
d89a826
Код
Авторство
О чём код?
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 TourAgencyAPI.Entities; namespace TourAgencyAPI.Controllers { [RoutePrefix("api")] public class HotelCommentsController : ApiController { private TourAgencyDBEntities db = new TourAgencyDBEntities(); // GET: api/HotelComments public IQueryable<HotelComment> GetHotelComment() { return db.HotelComment; } // GET: api/getHotelComments?hotelId=5 [HttpGet] [Route("getHotelComments")] public IHttpActionResult GetHotelComments(int hotelId) { // Query in the database, not in memory var hotelComments = db.HotelComment .Where(p => p.Id == hotelId) .ToList(); // Return 200 with results (empty list if none) return Ok(hotelComments); } // GET: api/HotelComments/5 [ResponseType(typeof(HotelComment))] public IHttpActionResult GetHotelComment(int id) { HotelComment hotelComment = db.HotelComment.Find(id); if (hotelComment == null) { return NotFound(); } return Ok(hotelComment); } // PUT: api/HotelComments/5 [HttpPut] [ResponseType(typeof(void))] public IHttpActionResult PutHotelComment(int id, HotelComment hotelComment) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != hotelComment.Id) { return BadRequest("ID mismatch"); } db.Entry(hotelComment).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!HotelCommentExists(id)) { return NotFound(); } throw; } return StatusCode(HttpStatusCode.NoContent); } // POST: api/HotelComments [HttpPost] [ResponseType(typeof(HotelComment))] public IHttpActionResult PostHotelComment(HotelComment hotelComment) { hotelComment.CreationDate = DateTime.Now; if (string.IsNullOrWhiteSpace(hotelComment.Author) || hotelComment.Author.Length > 100) ModelState.AddModelError("Author", "Author is required atring up to 100 symbols."); if (string.IsNullOrWhiteSpace(hotelComment.Text)) ModelState.AddModelError("Author", "Text is required atring."); if (!(db.Hotel.ToList().FirstOrDefault(p => p.Id == hotelComment.Id) is Hotel)) ModelState.AddModelError("HotelId", "HotelId is hotel's id from database."); if (!ModelState.IsValid) { return BadRequest(ModelState); } db.HotelComment.Add(hotelComment); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = hotelComment.Id }, hotelComment); } // DELETE: api/HotelComments/5 [HttpDelete] [ResponseType(typeof(HotelComment))] public IHttpActionResult DeleteHotelComment(int id) { HotelComment hotelComment = db.HotelComment.Find(id); if (hotelComment == null) { return NotFound(); } db.HotelComment.Remove(hotelComment); db.SaveChanges(); return Ok(hotelComment); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool HotelCommentExists(int id) { return db.HotelComment.Count(e => e.Id == id) > 0; } } }