Ejemplo n.º 1
0
        public async Task <IActionResult> SetMainPhoto(int userId, int photoId)
        {
            var photoFromRepo = await _repository.Get <Photo>(photoId);

            if (photoFromRepo == null)
            {
                return(NotFound("Could not find photo"));
            }

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("This is aleady the main photo"));
            }

            var mainPhoto = (await _repository.FindBy <Photo>(p => p.IsMain && p.UserId == userId)).FirstOrDefault();

            if (mainPhoto != null)
            {
                mainPhoto.IsMain = false;
            }

            photoFromRepo.IsMain = true;
            if (await _repository.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Could not set photo as main"));
        }