/
renix
/
TravelGuide
Обзор
Документация
Войти
/
renix
/
TravelGuide
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
back/Controllers/LandmarkRecomendationController.cs
63 строки
2 KB
atikinvobud
Испаравленные jwt токен и запрос создание и изменения оценки
15 мар 2025, 14:06
15 мар 2025, 14:06
97da6be
Код
Авторство
О чём код?
using System.Security.Claims; using System.Threading.Tasks; using back.DTOs.LandmarkRecomendationDTOs; using back.Extensions; using back.Models; using back.Models.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace back.Controllers; [ApiController] [Route("recomendation")] [Authorize] public class LandmarkRecomendationController : ControllerBase { private readonly Context context; public LandmarkRecomendationController(Context context) { this.context = context; } [HttpGet("")] public IActionResult Get() { List<GetLandmarkRecomendationDTO> landmarks = new List<GetLandmarkRecomendationDTO>(); foreach(LandmarkRecomendationEntity landmarkRecomendation in context.LandmarkRecomendations.ToList()) { landmarks.Add(landmarkRecomendation.ToDTO()); } return Ok(landmarks); } [HttpDelete("delete")] public IActionResult Delete([FromBody] DeleteLandmarkRecomendationDTO deleteLandmarkRecomendationDTO) { LandmarkRecomendationEntity? landmark = context.LandmarkRecomendations.Find(deleteLandmarkRecomendationDTO.Id); if(landmark == null) return NotFound(); context.LandmarkRecomendations.Remove(landmark); context.SaveChanges(); return Ok(); } [HttpPut("mark-upsert")] public async Task<IActionResult> Update([FromBody] List<PutLandmarkRecomendationDTO> putLandmarkRecomendationDTOs) { int UserId = int.Parse( User.FindFirstValue("userId")!); foreach(var putLandmark in putLandmarkRecomendationDTOs ) { List<LandmarkRecomendationEntity>? landmarkRecomendation =await context.LandmarkRecomendations.Where(lr => lr.LandmarkId == putLandmark.landmarkId && lr.UserId ==UserId).ToListAsync(); if(landmarkRecomendation.Count == 0) { LandmarkRecomendationEntity landmark = putLandmark.ToEntity(UserId); context.LandmarkRecomendations.Add(landmark); } else landmarkRecomendation![0].UpdateEntity(putLandmark, UserId, landmarkRecomendation[0].Id); context.SaveChanges(); } return Ok("Json обработан"); } }