Exemplo n.º 1
0
        public StorePoints CreateStorePoints(User clientUser, Store store, Post post)
        {
            // load store points or create new register and add the post points
            StorePoints storePoints = Context.StorePoints.FirstOrDefault(o => o.User == clientUser);

            if (storePoints == null)
            {
                storePoints = new StorePoints()
                {
                    User = clientUser, CreateTime = DateTime.Now, Store = store
                };
            }

            storePoints.Points    += post.Points;
            storePoints.UpdateTime = DateTime.Now;

            if (storePoints.Id == 0)
            {
                Context.Add(storePoints);
                Context.SaveChanges();
            }
            else
            {
                Context.Update(storePoints);
                Context.SaveChanges();
            }

            return(storePoints);
        }
Exemplo n.º 2
0
        public void SaveStorePointsHistory(StorePoints storePoints, Post post)
        {
            StorePointsHistory history = new StorePointsHistory {
                StorePoints = storePoints, Points = post.Points, Operation = "afegits", CreateTime = DateTime.Now
            };

            Context.Add(history);
            Context.SaveChanges();
        }
Exemplo n.º 3
0
        public StorePointsDto StorePointsToDto(StorePoints points)
        {
            UserDto  user  = UserMapper.UserToDto(points.User);
            StoreDto store = StoreMapper.StoreToDto(points.Store);

            StorePointsDto dto = new StorePointsDto {
                Store = store, User = user, CreateTime = DateTime.Now, Points = points.Points, UpdateTime = points.UpdateTime
            };

            return(dto);
        }
Exemplo n.º 4
0
        public IActionResult AddPoints(int postId, int userId)
        {
            // this user should be the admin of the store
            User user = GetUser();

            Post post = Context.Post.Include(o => o.Store).FirstOrDefault(o => o.Id == postId);

            // chack if the store is owned by the user who call the endpoint
            if (post.Store.User != user)
            {
                return(RedirectToAction("Index"));
            }


            //User to add the points
            User clientUser = Context.User.FirstOrDefault(o => o.Id == userId);

            StorePoints storePoints = StorePointsService.CreateStorePoints(clientUser, post.Store, post);

            StorePointsHistoryService.SaveStorePointsHistory(storePoints, post);

            return(RedirectToAction("Index"));
        }