/
lirikkrd
/
API
Обзор
Документация
Войти
/
lirikkrd
/
API
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Controllers/HotelCommentsController.cs
134 строки
4 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; namespace ToursWebApi.Controllers { public class HotelCommentsController : ApiController { private ToursBaseEntities db = new ToursBaseEntities(); // GET: api/HotelComments public IQueryable<HotelComment> GetHotelComments() { return db.HotelComments; } [Route("api/getHotelComments")] public IHttpActionResult GetHotelComments(int hotelId) { var hotelComments = db.HotelComments.ToList().Where(p => p.HotelId == hotelId).ToList(); return Ok(hotelComments); } // GET: api/HotelComments/5 [ResponseType(typeof(HotelComment))] public IHttpActionResult GetHotelComment(int id) { HotelComment hotelComment = db.HotelComments.Find(id); if (hotelComment == null) { return NotFound(); } return Ok(hotelComment); } // PUT: api/HotelComments/5 [ResponseType(typeof(void))] public IHttpActionResult PutHotelComment(int id, HotelComment hotelComment) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != hotelComment.Id) { return BadRequest(); } db.Entry(hotelComment).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!HotelCommentExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/HotelComments [ResponseType(typeof(HotelComment))] public IHttpActionResult PostHotelComment(HotelComment hotelComment) { hotelComment.CreationDate = DateTime.Now; if (string.IsNullOrWhiteSpace(hotelComment.Author) || hotelComment.Author.Length > 100) ModelState.AddModelError("Author", "Больше 100"); if (string.IsNullOrWhiteSpace(hotelComment.Text)) ModelState.AddModelError("Text", "Пусто"); if (!(db.Hotels.ToList().FirstOrDefault(p => p.Id == hotelComment.HotelId) is Hotel)) ModelState.AddModelError("HotelId", "Нет в базе"); if (!ModelState.IsValid) { return BadRequest(ModelState); } db.HotelComments.Add(hotelComment); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = hotelComment.Id }, hotelComment); } // DELETE: api/HotelComments/5 [ResponseType(typeof(HotelComment))] public IHttpActionResult DeleteHotelComment(int id) { HotelComment hotelComment = db.HotelComments.Find(id); if (hotelComment == null) { return NotFound(); } db.HotelComments.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.HotelComments.Count(e => e.Id == id) > 0; } } }