コード例 #1
0
        public ActionResult DeleteFriend(int id)
        {
            FriendModel m = friends.Friends.FirstOrDefault(f => f.Id == id);

            if (m != null)
            {
                friends.Friends.Remove(m);
            }
            friends.SaveChanges();
            return(View("ShowFriends", friends.Friends.ToList()));
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "Id,Name,Phone,Email,City")] Friend friend)
        {
            if (ModelState.IsValid)
            {
                db.Friends.Add(friend);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(friend);
        }
コード例 #3
0
        public ActionResult Create([Bind(Include = "Id,Ime,MestoZiveenje")] Friend friend)
        {
            if (ModelState.IsValid)
            {
                db.friends.Add(friend);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(friend));
        }
コード例 #4
0
        public void Insert(Friend entity)
        {
            var friend = new Friend()
            {
                Name       = entity.Name,
                FamilyName = entity.FamilyName,
                Birthdate  = entity.Birthdate
            };

            db.Friends.Add(friend);
            db.SaveChanges();
        }
コード例 #5
0
        // POST api/<controller>
        public HttpResponseMessage Post(Friend friend)
        {
            if (ModelState.IsValid)
            {
                db.Friends.Add(friend);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, friend);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = friend.FriendId }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
コード例 #6
0
 public FriendController(FriendsContext context)
 {
     _context = context;
     if (_context.Friends.Count() == 0)
     {
         _context.Friends.Add(new Friend {
             Name = "Item1"
         });
         _context.SaveChanges();
     }
 }
コード例 #7
0
        public IActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                post.DatePosted = DateTime.Now;

                var  authors = _context.Users.Where(u => u.Name.Equals(post.User.Name));
                User author  = null;
                if (authors.Any())
                {
                    author = authors.First();
                }
                else
                {
                    author = _context.Users.Add(new Models.User {
                        Name = post.Wall.User.Name, Wall = new Models.Wall()
                    }).Entity;
                }
                post.User = author;

                var walls = _context.Walls.Where(w => w.User.Name.Equals(post.Wall.User.Name));
                if (walls.Any())
                {
                    post.Wall = walls.First();
                }
                else
                {
                    User newUser = _context.Users.Add(new Models.User {
                        Name = post.Wall.User.Name, Wall = new Models.Wall()
                    }).Entity;
                    post.Wall = newUser.Wall;
                }
                _context.Posts.Add(post);

                _context.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(post));
        }
コード例 #8
0
 public IActionResult Edit(Friend friend)
 {
     ModelState.Clear();
     TryValidateModel(friend);
     if (ModelState.IsValid)
     {
         using (var db = new FriendsContext())
         {
             db.Friends.Update(friend);
             var count = db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     return(View(friend));
 }
コード例 #9
0
        public IActionResult Create(Friend friend)
        {
            ModelState.Clear();
            TryValidateModel(friend);
            if (ModelState.IsValid)
            {
                using (var db = new FriendsContext())
                {
                    // Create a channel
                    var channel = _client.CreateChannel(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), "public", friend.PhoneNumber, friend.PhoneNumber, string.Empty);

                    // join this channel
                    if (channel.RestException != null)
                    {
                        //an exception occurred making the REST call
                        return(Content(channel.RestException.Message));
                    }
                    else
                    {
                        // Create a user
                        var user = _client.CreateUser(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), friend.PhoneNumber.RemoveSpecialCharacters());
                        if (user.RestException != null)
                        {
                            return(Content(user.RestException.Message));
                        }
                        else
                        {
                            // Create membership
                            var member = _client.CreateMember(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), channel.Sid, user.Identity, string.Empty);
                            if (member.RestException != null)
                            {
                                return(Content(member.RestException.Message));
                            }
                            else
                            {
                                // Add complete user to the DB
                                friend.ChannelSid = channel.Sid;
                                friend.UserSid    = user.Sid;
                                db.Friends.Add(friend);
                                db.SaveChanges();
                            }
                        }
                    }
                    return(RedirectToAction("Index"));
                }
            }
            return(View(friend));
        }
コード例 #10
0
        public IActionResult Delete(int id)
        {
            var friend = _db.Friends.FirstOrDefault(s => s.FriendId == id);

            if (friend == null)
            {
                return(HttpNotFound());
            }
            else
            {
                // Delete channel
                _client.DeleteChannel(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), friend.ChannelSid);
                _client.DeleteUser(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), friend.UserSid);

                // Remove user from from the database
                _db.Friends.Remove(friend);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }