예제 #1
0
        public virtual MessageResult Message(string msg = null)
        {
            MessageResult result = this.GetService <MessageResult>();

            if (!string.IsNullOrEmpty(msg))
            {
                result.Add(msg);
            }
            return(result);
        }
        public IResult TakePhone(string phoneId, string currentUserId)
        {
            TakeOrderResultViewModel model = new TakeOrderResultViewModel();

            Phone phone = this.Data.Phones.All()
                          .Where(p => p.PhoneNumber == phoneId)
                          .FirstOrDefault();

            if (phone == null)
            {
                return(this.ErrorResult("Error: There is no such phone"));
            }

            if (phone.PhoneStatus != PhoneStatus.Taken)
            {
                return(this.ErrorResult(string.Format("Error: Phone status is {0}. It can be taken back.", phone.PhoneStatus.ToString())));
            }

            PhoneNumberOrder takeOrder = this.Data.PhoneNumberOrders.All()
                                         .Where(p => p.PhoneNumber == phoneId)
                                         .OrderByDescending(p => p.Id)
                                         .FirstOrDefault();

            if (takeOrder == null)
            {
                return(this.ErrorResult(string.Format("Error: There is no order for this phone {0}", phoneId)));
            }

            if (takeOrder.PhoneAction != PhoneAction.TakePhone)
            {
                return(this.ErrorResult(string.Format("Error: Order status is {0} for phone {1}. It can be taken back.",
                                                      takeOrder.PhoneAction.ToString(), phoneId)));
            }

            PhoneNumberOrder order = new PhoneNumberOrder()
            {
                ActionDate  = DateTime.Now,
                AdminId     = currentUserId,
                PhoneNumber = phoneId,
                UserId      = takeOrder.UserId,
                PhoneAction = PhoneAction.GiveBackPhone
            };

            User user = this.Data.Users.GetById(order.UserId);

            user.IsActive = true;

            this.Data.PhoneNumberOrders.Add(order);
            this.Data.Users.Update(user);
            phone.PhoneStatus = PhoneStatus.Free;
            phone.UserId      = null;
            this.Data.Phones.Update(phone);

            // TODO phone available after logic

            try
            {
                this.Data.SaveChanges();
            }
            catch (Exception ex)
            {
                ErrorResult errors = new ErrorResult();
                foreach (var key in ex.Data.Keys)
                {
                    errors.Add(key + " " + ex.Data[key]);
                }

                return(errors);
            }

            MessageResult message = new MessageResult();

            message.Add(string.Format("Order with id {0} was created", order.Id));
            message.Add(string.Format("Phone with id {0} was set to free and available after {1}", phone.PhoneNumber, phone.AvailableAfter));
            return(message);
        }