public IActionResult FollowUser(int userId)
        {
            var myUserId = User.CurrentUserId();

            if (db.User.Find(userId) == null)
            {
                return(BadRequest($"Cannot find UserId {userId}"));
            }


            // FollowRelation follow = new FollowRelation();
            var follow = db.FollowRelation.Where(r => r.From == myUserId && r.To == userId).FirstOrDefault();

            if (follow != null)
            {
                return(BadRequest("User already follows this user."));
            }
            follow            = new FollowRelation();
            follow.From       = myUserId;
            follow.To         = userId;
            follow.CreateDate = DateTime.Now;
            db.FollowRelation.Add(follow);
            db.SaveChanges();
            return(Ok("Following user successfully"));
        }
        public IActionResult LikePhoto(int postId)
        {
            var userId = User.CurrentUserId();

            if (postId < 0)
            {
                return(BadRequest("PostId claim error, cannot find PostID"));
            }

            UserLikePost like = new UserLikePost();

            like.UserId = userId;
            like.PostId = postId;
            Post post = db.Post.Where(p => p.Id == postId).FirstOrDefault();

            if (post == null)
            {
                return(BadRequest("PostId claim error, cannot find PostID"));
            }

            like.PostUserId = post.UserId;
            like.CreateDate = DateTime.Now;
            db.UserLikePost.Add(like);
            db.SaveChanges();
            return(Ok("like successfully"));
        }
Esempio n. 3
0
        public async System.Threading.Tasks.Task <IActionResult> UploadAsync(IFormFile file, CreatePost post)
        {
            var uploads = Path.Combine(env.WebRootPath, "photos");

            if (file.Length > 0)
            {
                var userId   = User.CurrentUserId();
                var FileName = $"{userId}{DateTime.Now.ToString("yyyy_MM_ddTHH_mm_ss")}{file.FileName}";
                using (var fileStream = new FileStream(Path.Combine(uploads, FileName), FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);

                    var createDate = DateTime.Now;
                    var newPost    = new Post
                    {
                        UserId     = userId,
                        Content    = post.Content,
                        Lati       = post.Lati,
                        Logi       = post.Logi,
                        Location   = post.Location,
                        CreateDate = createDate
                    };

                    var image = new Image
                    {
                        Post       = newPost,
                        UserId     = userId,
                        CreateDate = createDate,
                        ImageUrl   = FileName
                    };
                    try
                    {
                        db.Post.Add(newPost);
                        db.Image.Add(image);
                        db.SaveChanges();
                    }
                    catch (DbUpdateException e)
                    {
                        //This either returns a error string, or null if it can’t handle that error

                        Debug.WriteLine(e.ToString());
                        return(BadRequest("upload failed")); //return the error string

                        // throw; //couldn’t handle that error, so rethrow
                    }
                    return(Ok("Upload successfully"));
                }
            }
            return(BadRequest("Upload failed"));
        }
Esempio n. 4
0
        public ActionResult SendReview(Review review, float rating)
        {
            MobileDbContext db      = new MobileDbContext();
            string          session = Session[USER_SESSION].ToString();

            review.DatePost = DateTime.Now;
            //review.UserId = db.User.Single(a => a.UserName.Equals(session)).ID;
            review.Rating = rating;
            db.Review.Add(review);
            db.SaveChanges();
            return(RedirectToAction("Detail", "Product", new { id = review.ProductId }));
        }
Esempio n. 5
0
 public bool Insert(OrderDetail detail)
 {
     try
     {
         db.OrderDetail.Add(detail);
         db.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 6
0
        private void GetMobileServiceOnline(Subscription subscription)
        {
            MobileDbContext ctx            = new MobileDbContext();
            var             subscriptionDb = ctx.Subscriptions.Where(s => s.Id == subscription.Id).First();

            try
            {
                var service = Data.Helper.GetMobileServiceInSubscription(subscription.Id.ToString("d"));

                if (service == null)
                {
                    return;
                }

                service.ForEach(u =>
                {
                    if (!MobileServiceList.Contains(u.Name))
                    {
                        Application.Current.Dispatcher.Invoke(() => MobileServiceList.Add(u.Name));

                        subscriptionDb.MobileServices.Add(new MobileService()
                        {
                            Name = u.Name
                        });

                        try
                        {
                            ctx.SaveChanges();
                            Message = String.Format("Inserted New Mobile Service Reference : {0}", u.Name);
                        }
                        catch (Exception e) {
                            Message = String.Format("Error during Mobile Service List Update : {0}", e.Message);
                        }
                    }
                });
            }
            catch (Exception e) {
                Message = e.Message;
            }
        }
Esempio n. 7
0
        public IActionResult Signup([FromBody] SignupForm Signup)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (db.User.Where(u => u.Username == Signup.Username).FirstOrDefault() != null)
            {
                return(BadRequest($"Username {Signup.Username} already exists."));
            }

            Signup.Password = Crypto.HashPassword(Signup.Password);

            User newUser = new User(Signup);

            db.User.Add(newUser);

            db.SaveChanges();

            return(Ok("Signup successfully"));
        }
 public bool Save()
 {
     _dbContext.SaveChanges();
     return(true);
 }
Esempio n. 9
0
 //Create
 public long Insert(User entity)
 {
     db.User.Add(entity);
     db.SaveChanges();
     return(entity.ID);
 }
Esempio n. 10
0
 public int InsertFeedBack(Feedback fb)
 {
     db.Feedback.Add(fb);
     db.SaveChanges();
     return(fb.ID);
 }
Esempio n. 11
0
 public long Insert(Order order)
 {
     db.Order.Add(order);
     db.SaveChanges();
     return(order.ID);
 }
Esempio n. 12
0
 public void Save()
 {
     _context.SaveChanges();
 }
Esempio n. 13
0
        public void LoadPublishProfile(PublishDataPublishProfile profile_)
        {
            new DelegateCommandBW((o) =>
            {
                try
                {
                    var profile        = o as PublishDataPublishProfile;
                    LoadingRingMessage = true;
                    Message            = "Loading Publish Profile";
                    if (profile.Subscription.Count() != 0)
                    {
                        var _sub = new List <Subscription>();

                        MobileDbContext ctx = new MobileDbContext();

                        foreach (var subscription in profile.Subscription)
                        {
                            _sub.Add(new Subscription()
                            {
                                Id   = Guid.Parse(subscription.Id),
                                Name = subscription.Name,
                                Cert = subscription.ManagementCertificate
                            });

                            var subId = Guid.Parse(subscription.Id);
                            //Check If subscription already exist in the database
                            if (!ctx.Subscriptions.Any(u => u.Id == subId))
                            {
                                Message         = String.Format("New Subscription : {0} added", subscription.Name);
                                var subToInsert = new Subscription()
                                {
                                    Id   = subId,
                                    Name = subscription.Name,
                                    Cert = subscription.ManagementCertificate
                                };
                                ctx.Subscriptions.Add(subToInsert);
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    Subscriptions.Add(subToInsert);
                                });
                            }
                            //Else Check if the certificate has changed, take the last certificate
                            else if (ctx.Subscriptions.Find(subId) != null)
                            {
                                var sub = ctx.Subscriptions.Find(subId);
                                if (sub.Cert != subscription.ManagementCertificate)
                                {
                                    sub.Cert = subscription.ManagementCertificate;
                                    Message  = String.Format("Certificate change for Subscription :{0}",
                                                             subscription.Name);
                                }
                            }
                        }
                        ctx.SaveChanges();
                        Message = "Publish Profile Loaded";
                    }
                    else
                    {
                        Message = "No subscription found in the publish profile";
                    }
                }
                catch (Exception e)
                {
                    Message = e.Message;
                }

                finally
                {
                    LoadingRingMessage = false;
                }
            }).Execute(profile_);
        }
Esempio n. 14
0
 //Create
 public long Insert(Product entity)
 {
     db.Product.Add(entity);
     db.SaveChanges();
     return(entity.ID);
 }