Ejemplo n.º 1
0
        public async Task <IActionResult> SetMainPhoto(int id, int userid)
        {
            //throw new Exception("Asdf");
            //check to see if userid and jwt id are equal.
            var claimid = int.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value);

            if (userid != claimid)
            {
                return(Unauthorized());
            }
            var user = await rep.GetUser(userid);

            // check to see if the photo exists
            if (!user.Photos.Any(e => e.Id == id))
            {
                return(Unauthorized());
            }
            // check to see if the photo is already the main photo
            var photo = user.Photos.FirstOrDefault(e => e.Id == id);

            if (photo.IsMain)
            {
                return(BadRequest("this photo is already the main photo!"));
            }
            var mainPhoto = rep.FindMainPhoto(user);

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

            if (await rep.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception("saving changes to photo is not possible!");
        }