예제 #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 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 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));
        }