public async Task <IActionResult> Edit(int id, [Bind("Id,Name,EnteredBy,EnteredOn")] FavoriteBand favoriteBand)
        {
            if (id != favoriteBand.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(favoriteBand);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FavoriteBandExists(favoriteBand.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(favoriteBand));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,EnteredBy,EnteredOn")] FavoriteBand favoriteBand)
        {
            if (ModelState.IsValid)
            {
                _context.Add(favoriteBand);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(favoriteBand));
        }
        public async Task <IActionResult> Create([Bind("Id,Name")] FavoriteBand favoriteBand)
        {
            if (ModelState.IsValid)
            {
                favoriteBand.EnteredOn = DateTime.Now;
                favoriteBand.EnteredBy = User.Identity.Name;
                _context.Add(favoriteBand);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(favoriteBand));
        }
Exemplo n.º 4
0
        public FavoriteBand AddFavoriteBand([FromBody] UserIdName userIdName)
        {
            string userId   = userIdName.userId;
            string bandName = userIdName.name;
            GraphClientConnection graphClient  = new GraphClientConnection();
            FavoriteBand          favoriteBand = new FavoriteBand();

            if (graphClient == null)
            {
                StatusCode(500);
                return(null);
            }

            string matchQuery        = "(user:User{id:'" + userId + "'})-[like:LIKE]->(favoriteBand:Band{name:'" + bandName + "'})";
            var    favoriteBandQuery = graphClient.client.Cypher
                                       .Match(matchQuery)
                                       .Return((favoriteBand) => new {
                FavoriteBand = favoriteBand.As <FavoriteBand>()
            })
                                       .Results;

            if (favoriteBandQuery.Count() == 1) // relation between nodes exist already, so, you need to delete that relation
            {
                graphClient.client.Cypher
                .Match(matchQuery)
                .Delete("like")
                .ExecuteWithoutResults();
                //204
                return(null);
            }

            var newFavoriteBandQuery = graphClient.client.Cypher
                                       .Match("(user:User{id:'" + userId + "'}),(favoriteBand:Band{name:'" + bandName + "'})")
                                       .Create("(user)-[:LIKE]->(favoriteBand)")
                                       .Return((favoriteBand) => new {
                FavoriteBand = favoriteBand.As <FavoriteBand>()
            })
                                       .Results;

            favoriteBand.name     = newFavoriteBandQuery.ToList()[0].FavoriteBand.name;
            favoriteBand.type     = newFavoriteBandQuery.ToList()[0].FavoriteBand.type;
            favoriteBand.imageUrl = newFavoriteBandQuery.ToList()[0].FavoriteBand.imageUrl;
            return(favoriteBand);
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] FavoriteBand favoriteBand)
        {
            if (id != favoriteBand.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var existingBand = await _context.FavoriteBands.FindAsync(id);

                    if (existingBand.EnteredBy != User.Identity.Name)
                    {
                        return(Unauthorized());
                    }
                    existingBand.Name = favoriteBand.Name;
                    _context.Update(existingBand);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FavoriteBandExists(favoriteBand.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(favoriteBand));
        }