Exemplo n.º 1
0
 public IActionResult Register(ViewUser newUser)
 {
     if (ModelState.IsValid)
     {
         if (_context.Users.Where(user => user.Email == newUser.Email).Count() != 0)
         {
             ModelState.AddModelError("Email", "Email already exists!");
         }
         else
         {
             PasswordHasher <User> hasher = new PasswordHasher <User>();
             User user = new User
             {
                 Name        = newUser.Name,
                 Email       = newUser.Email,
                 Description = newUser.Description
             };
             user.PasswordHash = hasher.HashPassword(user, newUser.Password);
             _context.Users.Add(user);
             _context.SaveChanges();
             user.created_at = DateTime.Now;
             user.updated_at = DateTime.Now;
             _context.SaveChanges();
             HttpContext.Session.SetInt32("currUser", user.UserId);
             return(Redirect("/professional_profile"));
         }
     }
     return(View("Index", newUser));
 }
Exemplo n.º 2
0
        public ActionResult Create([Bind(Include = "GroupId,Title,Content")] Post post)
        {
            if (ModelState.IsValid)
            {
                User user = db.Users.Find(Convert.ToInt32(Session["UserID"]));
                post.User     = user;
                post.PostDate = DateTime.Now;

                if (post.GroupId == 0)
                {
                    post.GroupId = null;
                    db.Posts.Add(post);
                    db.SaveChanges();
                    return(RedirectToAction("Home", "Users"));
                }

                Group group = db.Groups.Find(post.GroupId);
                group.Posts.Add(post);
                db.Entry(group).State = group.GroupID == 0 ?
                                        EntityState.Added :
                                        EntityState.Modified;

                db.SaveChanges();
                return(RedirectToAction("ViewGroup", "Groups", new { groupId = group.GroupID }));
            }

            return(View(post));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Добавить пост
 /// </summary>
 /// <param name="user"></param>
 /// <param name="foodId"></param>
 public void AddPost(User user, int foodId)
 {
     ctx.Posts.Add(new Post()
     {
         UserId = user.Id,
         FoodId = foodId,
         Date   = DateTime.Now
     });
     ctx.SaveChanges();
 }
Exemplo n.º 4
0
        public ActionResult Create([Bind(Include = "AdminId,Title")] Group group)
        {
            if (ModelState.IsValid)
            {
                group.CreatedDate = DateTime.Now;
                User groupMemberAlsoAdmin = db.Users.Find(group.AdminId);
                group.Members = new List <User>();
                group.Posts   = new List <Post>();
                group.Members.Add(groupMemberAlsoAdmin);
                db.Groups.Add(group);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(group));
        }
Exemplo n.º 5
0
        public IActionResult Register(User user, string PasswordConfirm)
        {
            if (user.Password != PasswordConfirm)
            {
                ViewBag.PasswordError = "Passwords don't match";
                return(View("Index"));
            }
            if (ModelState.IsValid)
            {
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                user.Password = Hasher.HashPassword(user, user.Password);
                User ExistingEmail = _context.users.SingleOrDefault(u => u.Email == user.Email);
                if (ExistingEmail != null)
                {
                    ViewBag.RegError = "Email already registered";
                    return(View("Index"));
                }
                else
                {
                    _context.Add(user);
                    _context.SaveChanges();

                    int?UserId = user.UserId;
                    ViewBag.CurrentUser = user;
                    HttpContext.Session.SetInt32("UserId", (int)UserId);
                    HttpContext.Session.SetString("UserEmail", (string)user.Email);
                    HttpContext.Session.SetString("UserName", (string)user.Name);
                    return(RedirectToAction("Profile"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }
        public IActionResult Connect(int id)
        {
            if (!isLoggedIn())
            {
                return(Redirect("/"));
            }
            User currUser = _context.Users
                            .Include(user => user.ConnectedTo).ThenInclude(conn => conn.ConnectedTo)
                            .Include(user => user.ConnectedBy).ThenInclude(conn => conn.Connector)
                            .Single(user => user.UserId == (int)HttpContext.Session.GetInt32("currUser"));

            Console.WriteLine(currUser.ConnectedBy.Count);
            Console.WriteLine(currUser.ConnectedTo.Count);
            try
            {
                User connectUser = _context.Users
                                   .Include(user => user.ConnectedTo).ThenInclude(conn => conn.ConnectedTo)
                                   .Include(user => user.ConnectedBy).ThenInclude(conn => conn.Connector)
                                   .Where(user => user.UserId != currUser.UserId)
                                   .Where(user => UserFilter(user, currUser)).Single(user => user.UserId == id);
            }
            catch
            {
                return(RedirectToAction("Profile"));
            }
            Connection connect = new Connection
            {
                ConnectorId   = currUser.UserId,
                ConnectedToId = id,
                Accepted      = 0
            };

            _context.Connections.Add(connect);
            _context.SaveChanges();
            return(RedirectToAction("Profile"));
        }
Exemplo n.º 7
0
        public ActionResult Create([Bind(Include = "Content,PostId")] Comment comment)
        {
            User user = db.Users.Find(GetUserIdFromSession());
            Post post = db.Posts.Find(comment.PostId);

            if (ModelState.IsValid)
            {
                comment.User = user;
                comment.Post = post;
                db.Comments.Add(comment);
                db.SaveChanges();

                if (Session["GroupRedirectId"] != null)
                {
                    int SessionGroupId = Convert.ToInt32(Session["GroupRedirectId"]);
                    Session["GroupRedirectId"] = null;
                    return(RedirectToAction("ViewGroup", "Groups", new { groupId = SessionGroupId }));
                }

                return(RedirectToAction("Home", "Users"));
            }

            return(View(comment));
        }
Exemplo n.º 8
0
        public ActionResult Register([Bind(Include = "UserID,FirstName,LastName,Password,Gender,Email,BirthDate,ImageUrl,IsAdmin,Address")] User user)
        {
            if (ModelState.IsValid)
            {
                if (EmailAlreadyTaken(user.Email))
                {
                    ViewBag.ErrorMsg = "Email already taken";
                    return(View(user));
                }

                user.IsAdmin = false;
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Home", "Users"));
            }

            return(View(user));
        }
Exemplo n.º 9
0
        public ActionResult Edit([Bind(Include = "UserId,FirstName,LastName,Gender,Email,BirthDate,Password,Address")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                if (Session["Admin"] == null)
                {
                    return(RedirectToAction("Logout", "Welcome"));
                }
                else
                {
                    return(RedirectToAction("ManageUsers", "Admin"));
                }
            }

            return(View(user));
        }
        static void Main(string[] args)
        {
            using (var context = new NetworkContext())
            {
                var user = new User()
                {
                    Name = "Ippolit", Age = 44
                };
                var photo = new Photo()
                {
                    Description = "Nature"
                };
                var comment = new Comment()
                {
                    Text = "Cool))", PhotoId = 1, UserId = 1
                };

                context.Users.Add(user);
                context.Photos.Add(photo);
                context.Comments.Add(comment);
                context.SaveChanges();
            }
        }
Exemplo n.º 11
0
        private Guid ProcessSave(CNN network, string name, NetworkContext context)
        {
            var cnnModel = new CnnModel
            {
                CnnModelId = Guid.NewGuid()
            };
            var layers     = new List <CnnLayer>();
            var cnnWeights = new List <CnnWeights>();

            for (int i = 0; i < network.LayersCount; i++)
            {
                if (network.Layers[i] is ConvolutionalLayer)
                {
                    var cnnLayer = network.Layers[i] as ConvolutionalLayer;
                    var layer    = new CnnLayer
                    {
                        CnnLayerId         = Guid.NewGuid(),
                        PositionIn         = i,
                        KernelHeight       = cnnLayer.KernelSize,
                        KernelWidth        = cnnLayer.KernelSize,
                        KernelsCount       = cnnLayer.KernelsCount,
                        FeatureMapsCountIn = cnnLayer.KernelDepth,
                        LayerType          = (byte)LayerType.CovolutionalLayer,
                        Model = cnnModel
                    };

                    var weights = new CnnWeights
                    {
                        CnnWeightsId = Guid.NewGuid(),
                        Layer        = layer,
                        LayerId      = layer.CnnLayerId
                    };

                    var w       = string.Empty;
                    var builder = new StringBuilder(String.Empty);
                    for (int j = 0; j < cnnLayer.KernelsCount; ++j)
                    {
                        for (int k = 0; k < cnnLayer.KernelDepth; ++k)
                        {
                            for (int a = 0; a < cnnLayer.Kernels[j][k].Length; ++a)
                            {
                                for (int b = 0; b < cnnLayer.Kernels[j][k][a].Length; ++b)
                                {
                                    builder.Append(cnnLayer.Kernels[j][k][a][b] + ";");
                                }
                            }
                        }
                    }

                    w = builder.ToString();
                    weights.Weights = w;
                    cnnWeights.Add(weights);

                    layer.Weights = weights;

                    layers.Add(layer);
                }
                else if (network.Layers[i] is PollingLayer)
                {
                    var cnnLayer = network.Layers[i] as PollingLayer;
                    var layer    = new CnnLayer
                    {
                        CnnLayerId   = Guid.NewGuid(),
                        PositionIn   = i,
                        KernelHeight = cnnLayer.KernelSize,
                        KernelWidth  = cnnLayer.KernelSize,
                        LayerType    = (byte)LayerType.PoolingLayer,
                        Model        = cnnModel
                    };

                    layers.Add(layer);
                }
                else
                {
                    var cnnLayer = network.Layers[i] as ReLuLayer;
                    var layer    = new CnnLayer
                    {
                        CnnLayerId   = Guid.NewGuid(),
                        PositionIn   = i,
                        KernelHeight = cnnLayer.KernelSize,
                        KernelWidth  = cnnLayer.KernelSize,
                        LayerType    = (byte)LayerType.ReluLayer,
                        Model        = cnnModel
                    };

                    layers.Add(layer);
                }
            }

            var l = new CnnLayer
            {
                CnnLayerId   = Guid.NewGuid(),
                KernelHeight = network.FlattenLayer.KernelSize,
                KernelWidth  = network.FlattenLayer.KernelSize,
                LayerType    = (byte)LayerType.FlattenLayer,
                Model        = cnnModel
            };

            layers.Add(l);

            cnnModel.Layers = layers;

            var perceptronModel = new PerceptronModel
            {
                PerceptronModelId = Guid.NewGuid()
            };
            var percLayers  = new List <PerceptronLayer>();
            var percWeights = new List <PerceptronWeights>();

            for (var i = 0; i < network.Perceptron.LayersCount; ++i)
            {
                var layer = network.Perceptron.Layers[i];

                var perLayer = new PerceptronLayer
                {
                    PerceptronLayerId = Guid.NewGuid(),
                    NeuronsCount      = layer.NeuronsCount,
                    PositionIn        = i,
                    Perceptron        = perceptronModel
                };

                var weights = new PerceptronWeights
                {
                    PerceptronWeightsId = perLayer.PerceptronLayerId,
                    Height = layer.WeightRowsCount,
                    Width  = layer.WeightColumnsCount
                };

                string w       = String.Empty;
                var    builder = new StringBuilder(String.Empty);
                for (int a = 0; a < layer.Weights.Length; ++a)
                {
                    for (int b = 0; b < layer.Weights[a].Length; ++b)
                    {
                        builder.Append(layer.Weights[a][b] + ";");
                    }
                }

                w = builder.ToString();

                weights.Weights = w;
                percWeights.Add(weights);

                percLayers.Add(perLayer);
            }

            perceptronModel.Layers = percLayers;

            //save
            var networkModel = new NetworkModel
            {
                NetworkModelId = Guid.NewGuid(),
                Perceptron     = perceptronModel,
                PerceptronId   = perceptronModel.PerceptronModelId,
                Cnn            = cnnModel,
                CnnId          = cnnModel.CnnModelId,
                Name           = name
            };

            cnnModel.NetworkModel   = networkModel;
            cnnModel.NetworkModelId = networkModel.NetworkModelId;

            perceptronModel.NetworkModel   = networkModel;
            perceptronModel.NetworkModelId = networkModel.NetworkModelId;

            context.NetworkModels.Add(networkModel);

            context.CnnLayers.AddRange(layers);
            context.CnnWeightsSet.AddRange(cnnWeights);
            context.CnnModels.Add(cnnModel);

            context.PerceptronLayers.AddRange(percLayers);
            context.PerceptronWeights.AddRange(percWeights);
            context.PerceptronModels.Add(perceptronModel);


            context.SaveChanges();

            return(networkModel.NetworkModelId);
        }
Exemplo n.º 12
0
        private Guid ProcessUpdate(CNN network, string name, NetworkContext context)
        {
            var cnnModel = context.NetworkModels.FirstOrDefault(item => item.Name == name);

            var layers = context.CnnLayers
                         .Where(layer => layer.ModelId == cnnModel.CnnId && layer.LayerType != 5)
                         .OrderBy(layer => layer.PositionIn).ToList();

            var cnnWeights = new List <CnnWeights>();

            for (int i = 0; i < network.LayersCount; i++)
            {
                if (network.Layers[i] is ConvolutionalLayer)
                {
                    var layerId = layers[i].CnnLayerId;
                    var weights = context.CnnWeightsSet.FirstOrDefault(item => item.LayerId == layerId);

                    var currentCnnLayer = network.Layers[i] as ConvolutionalLayer;

                    var w       = string.Empty;
                    var builder = new StringBuilder(String.Empty);
                    for (int j = 0; j < currentCnnLayer.KernelsCount; ++j)
                    {
                        for (int k = 0; k < currentCnnLayer.KernelDepth; ++k)
                        {
                            for (int a = 0; a < currentCnnLayer.Kernels[j][k].Length; ++a)
                            {
                                for (int b = 0; b < currentCnnLayer.Kernels[j][k][a].Length; ++b)
                                {
                                    builder.Append(currentCnnLayer.Kernels[j][k][a][b] + ";");
                                }
                            }
                        }
                    }

                    weights.Weights = builder.ToString();
                    cnnWeights.Add(weights);
                }
            }

            var perceptronModel = context.PerceptronModels
                                  .FirstOrDefault(item => item.NetworkModelId == cnnModel.NetworkModelId);

            var dbPerceptronLayers = context.PerceptronLayers
                                     .Where(item => item.PerceptronModelId == perceptronModel.PerceptronModelId)
                                     .OrderBy(l => l.PositionIn).ToList();

            var percWeights = new List <PerceptronWeights>();

            for (int i = 0; i < network.Perceptron.Layers.Count; ++i)
            {
                var percLayerId = dbPerceptronLayers[i].PerceptronLayerId;
                var weights     = context.PerceptronWeights
                                  .FirstOrDefault(l => l.PerceptronWeightsId == percLayerId);

                var currentLayer = network.Perceptron.Layers[i];

                string w       = String.Empty;
                var    builder = new StringBuilder(String.Empty);
                for (int a = 0; a < currentLayer.Weights.Length; ++a)
                {
                    for (int b = 0; b < currentLayer.Weights[a].Length; ++b)
                    {
                        builder.Append(currentLayer.Weights[a][b] + ";");
                    }
                }

                weights.Weights = builder.ToString();

                percWeights.Add(weights);
            }

            context.SaveChanges();

            return(Guid.Empty);
        }
Exemplo n.º 13
0
 public void Save()
 {
     _context.SaveChanges();
 }