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); }
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(); }
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); }
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")); }