Beispiel #1
0
        // [Consumes("application/json")]
        // [Produces("application/json")]
        public IActionResult UpdateCard([FromForm] UpdateRequest updateRequest)
        {
            try
            {
                Console.WriteLine(updateRequest.id);

                using var context = new flashcardsContext();

                var card = context.cards.First(c => c.id == updateRequest.id &&
                                               c.ownerId == LoggedInId());

                //db.GetCardById(updateRequest.id);
                Console.WriteLine("check after card");
                var what = updateRequest.what;
                var to   = updateRequest.to;
                switch (what.ToLower())
                {
                case "question":
                    card.question = to;
                    break;

                case "answer":
                    card.answer = to;
                    break;

                case "image":
                    card.picture = SaveImage(updateRequest.image, card.id);
                    break;

                default:
                    return(BadRequest($"{what} isn't a proper value"));
                }
                context.Update(card);
                context.SaveChanges();

                return(Ok());
            }
            catch (InvalidOperationException)
            {
                return(Unauthorized("Access denied"));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
Beispiel #2
0
        public IActionResult UpdateUserData(UpdateRequest updateRequest)
        {
            if (updateRequest.id != LoggedInId())
            {
                return(Unauthorized("Access denied"));
            }

            using var context = new flashcardsContext();
            var user = context.users.First(u => u.Id == updateRequest.id);

            var to   = updateRequest.to;
            var what = updateRequest.what;

            switch (what.ToLower())
            {
            case "email":
                if (!IsValidEmail(to))
                {
                    return(BadRequest($"'{to}' isn't correct email format."));
                }
                user.Email = to;
                break;

            case "name":
                user.name = to;
                break;

            case "surname":
                user.surname = to;
                break;

            case "password":
                user.ChangePassword(to);
                break;

            default:
                return(BadRequest($"'{what}' is not valid property"));
            }

            context.Update(user);
            context.SaveChanges();

            return(Ok("Successfully changed"));
        }