Esempio n. 1
0
        public Cart Create(Cart entity, User user, List <CartItem> items)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (!entity.Validate())
            {
                throw new ValidationException(entity.ValidationResults);
            }

            Cart createdEntity = null;

            using (IUnitOfWork uow = RepositoryFactory.CreateUnitOfWork())
            {
                createdEntity = repository.Create(entity);

                CartOwner cartOwner = CartOwner.Create(createdEntity, user, 0);
                CartOwnerService.Obj.Create(cartOwner);

                Service.User.ItemService.Obj.MultipleCreate(items, createdEntity.CartId);

                uow.Complete();
            }

            if (createdEntity == null)
            {
                throw new RepositoryException("Entity not created");
            }

            Tracer.Log.EntityCreated(nameof(Cart), createdEntity.CartId);

            return(createdEntity);
        }
        public async Task <IHttpActionResult> MakeSharing(ShareBindingModel model)
        {
            string userCode = UserCodeOperation.ProduceCode(model.CartId, (int)model.AccessLevel);

            foreach (var email in model.Emails)
            {
                User user = UserService.Obj.FindByEmail(email);

                if (user != null)
                {
                    Cart cart = CartMobileService.Obj.GetById(model.CartId);

                    CartOwner cartOwner = CartOwner.Create(cart, user, AccessLevel.ReadWrite);

                    CartOwnerMobileService.Obj.Create(cartOwner);

                    await UserService.Obj.SendEmailToShareCard(user.Email, userCode);
                }

                else
                {
                    await UserService.Obj.SendEmailToShareCard(email, userCode);
                }
            }

            var users = CartOwnerMobileService.Obj.GetUsersByCart(model.CartId).Select(x => x.ToUserProfileBindingModel(UserService.Obj.FindById(x.UserId), ImageService.Obj.FindByUserId(x.UserId))).ToList();

            return(Ok(users));
        }
        public static bool IsCartOwnerUnique(this CartOwner model)
        {
            IGenericQueryRepository queryRepository = RepositoryFactory.CreateQueryRepository();

            IQueryConstraints <CartOwner> constraints = new QueryConstraints <CartOwner>()
                                                        .AndAlso(x => x.CartId == model.CartId)
                                                        .AndAlso(x => x.UserId == model.UserId);

            return(queryRepository.SingleOrDefault(constraints) == null);
        }
Esempio n. 4
0
 public static UserProfileViewModel ToUserProfileViewModel(this CartOwner model, User user, Image image)
 {
     return(new UserProfileViewModel()
     {
         UserId = user.Id,
         Email = user.Email,
         Mobile = user.Mobile,
         Name = user.Name,
         AccessLevel = model.AccessLevel
     });
 }
Esempio n. 5
0
 public static UserProfileBindingModel ToUserProfileBindingModel(this CartOwner model, User user, Image image)
 {
     return(new UserProfileBindingModel()
     {
         UserId = user.Id,
         Email = user.Email,
         Mobile = user.Mobile,
         Name = user.Name,
         AccessLevel = model.AccessLevel,
         PhotoId = image?.ImageId
     });
 }
        public CartOwner Update(CartOwner entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (!entity.Validate())
            {
                throw new ValidationException(entity.ValidationResults);
            }

            CartOwner uptadedEntity = repository.Update(entity);

            Tracer.Log.EntityUpdated(nameof(CartOwner), entity.CartOwnerId);

            return(uptadedEntity ?? entity);
        }
        public IHttpActionResult RemoveCard(int cartId)
        {
            User user = GetCurrentUser();

            CartOwner cartOwner = CartOwnerMobileService.Obj.GetCartUser(cartId, user.Id);

            if (cartOwner.AccessLevel == AccessLevel.Owner)
            {
                CartMobileService.Obj.Delete(cartId);
            }

            else
            {
                CartOwnerMobileService.Obj.LeaveFromCurrentCart(cartOwner.CartId);
            }

            return(Ok());
        }
        public ActionResult GetAccess(string id)
        {
            try
            {
                int[] parameters = UserCodeOperation.DecodeCode(id);

                int         cartId      = parameters[0];
                AccessLevel accessLevel = (AccessLevel)parameters[1];

                if (accessLevel == AccessLevel.Owner)
                {
                    throw new BusinessRuleException("Owner access level must not be selected");
                }

                User user = GetCurrentUser();

                CartOwner currentCartUser = CartOwnerService.Obj.GetCartUser(cartId, user.Id);

                if (currentCartUser != null)
                {
                    if (currentCartUser.AccessLevel == AccessLevel.Owner)
                    {
                        return(RedirectToAction("Index"));
                    }
                }

                FriendService.Obj.CreateFriends(user.Id, cartId);

                Cart cart = CartService.Obj.GetById(cartId);

                CartOwner cartOwner = CartOwner.Create(cart, user, accessLevel);

                CartOwnerService.Obj.Create(cartOwner);

                return(RedirectToAction("Index"));
            }

            catch (Exception ex)
            {
                TempData["ErrorMessage"] = ex.Message;

                return(RedirectToAction("Login", "Account"));
            }
        }
        public CartOwner GetCartUser(int cartId, int ownerId)
        {
            if (cartId == 0)
            {
                throw new ArgumentNullException(nameof(cartId));
            }

            if (ownerId == 0)
            {
                throw new ArgumentNullException(nameof(ownerId));
            }

            IQueryConstraints <CartOwner> constraints = new QueryConstraints <CartOwner>()
                                                        .AndAlso(x => x.CartId == cartId)
                                                        .AndAlso(x => x.UserId == ownerId);

            CartOwner founded = queryRepository.SingleOrDefault(constraints);

            return(founded);
        }
        public CartOwner Create(CartOwner entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (!entity.Validate())
            {
                throw new ValidationException(entity.ValidationResults);
            }

            CartOwner cartOwner = GetCartUser(entity.CartId, entity.UserId);

            if (cartOwner != null)
            {
                if (cartOwner.AccessLevel == entity.AccessLevel)
                {
                    return(cartOwner);
                }
                else
                {
                    cartOwner.UpdateAccessLevel(entity.AccessLevel);
                    Update(cartOwner);
                    return(cartOwner);
                }
            }

            CartOwner createdEntity = repository.Create(entity);

            if (createdEntity == null)
            {
                throw new RepositoryException("Entity not created");
            }

            Tracer.Log.EntityCreated(nameof(CartOwner), createdEntity.CartId);

            return(createdEntity);
        }
        public IHttpActionResult GetAccess([FromBody] GetAccessBindingModel model)
        {
            int[]       parameters  = UserCodeOperation.DecodeCode(model.Id);
            int         cartId      = parameters[0];
            AccessLevel accessLevel = (AccessLevel)parameters[1];

            User user = GetCurrentUser();

            FriendService.Obj.CreateFriends(user.Id, cartId);

            Cart cart = CartMobileService.Obj.GetById(cartId);

            CartOwner currentCartUser = CartOwnerMobileService.Obj.GetCartUser(cartId, user.Id);

            if (currentCartUser != null)
            {
                if (currentCartUser.AccessLevel == AccessLevel.Owner)
                {
                    CartIndexBindingModel earlyBindingModel = getCartIndexBindingModel(cart);

                    return(Ok(earlyBindingModel));
                }
            }

            CartOwner cartOwner = CartOwner.Create(cart, user, accessLevel);

            double cartIndex = CartOwnerMobileService.Obj.GetLastIndex(user.Id);

            cartOwner.SetCartIndex(cartIndex);

            CartOwnerMobileService.Obj.Create(cartOwner);

            CartIndexBindingModel bindingModel = getCartIndexBindingModel(cart);

            return(Ok(bindingModel));
        }
        public async Task <IHttpActionResult> CreateCard(CreateCartBindingModel model)
        {
            User user = GetCurrentUser();

            Cart cart = null;

            if (!model.CartId.HasValue)
            {
                cart = Cart.Create(model.Title, model.Notes, model.Date);

                List <CartItem> items = model.Items?.Select(x => CartItem.Create(cart, Item.Create(x.Title, user), x.Status.Value)).ToList();

                cart = CartMobileService.Obj.Create(cart, user, items);

                // Make Sharing

                if (model.Users?.Count != 0 && model.Users != null)
                {
                    string userCode = UserCodeOperation.ProduceCode(cart.CartId, (int)AccessLevel.ReadWrite);

                    List <string> emails = model.Users.Select(x => x.Email).ToList();

                    User userToFind = null;

                    foreach (var email in emails)
                    {
                        if (email != user.Email)
                        {
                            userToFind = UserService.Obj.FindByEmail(email);

                            if (userToFind != null)
                            {
                                Cart cartToFind = CartMobileService.Obj.GetById(cart.CartId);

                                CartOwner cartOwner = CartOwner.Create(cartToFind, userToFind, AccessLevel.ReadWrite);

                                CartOwnerMobileService.Obj.Create(cartOwner);

                                await UserService.Obj.SendEmailToShareCard(userToFind.Email, userCode);
                            }

                            else
                            {
                                await UserService.Obj.SendEmailToShareCard(email, userCode);
                            }
                        }
                    }
                }
            }

            else
            {
                cart = CartMobileService.Obj.GetById(model.CartId.Value);
                cart.Update(model.Title, model.Notes, model.Date);

                List <CartOwner> users = new List <CartOwner>();

                if (model.Users != null)
                {
                    users = model.Users.Select(x => CartOwner.Create(cart, UserService.Obj.FindById(x.UserId), x.AccessLevel)).ToList();
                }

                List <CartItem> items = model.Items?.Select(x => CartItem.Create(cart, Item.Create(x.Title, user), x.Status.Value)).ToList();

                users.Add(CartOwner.Create(cart, user, AccessLevel.Owner));

                CartMobileService.Obj.Update(cart, items, users);
            }

            CartIndexBindingModel bindingModel = getCartIndexBindingModel(cart);

            return(Ok(bindingModel));
        }