Esempio n. 1
0
        public IHttpActionResult Create(CreateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            UserInt user    = UserIntFactory.CreateFromIdentity(_uow, User);
            Vehicle vehicle = model.GetVehicle(user);

            _uow.GetRepository <IVehicleRepository>().Add(vehicle);
            _uow.Commit();
            _uow.RefreshAllEntities();

            Blog blog = new Blog
            {
                Vehicle   = vehicle,
                VehicleId = vehicle.VehicleId,
                AuthorId  = user.Id,
                Name      = vehicle.Make + " " + vehicle.Model // TODO :: ugly
            };

            _uow.GetRepository <IBlogRepository>().Add(blog);
            _uow.Commit();

            if (_userManager.IsInRole(User.Identity.GetUserId <int>(), "CarOwner") == false)
            {
                _userManager.AddToRole(User.Identity.GetUserId <int>(), "CarOwner");
            }

            return(Ok(model));
        }
Esempio n. 2
0
        public ActionResult Create([Bind(Include = "Title,Text")] BLL.ViewModels.MessageThread.CreateModel createModel, int?receiverId)
        {
            UserInt user = _uow.GetRepository <IUserIntRepository>().GetById(receiverId);

            if (user == null && User.Identity.GetUserId <int>() != receiverId)
            {
                return(HttpNotFound());
            }


            if (ModelState.IsValid)
            {
                UserInt        sender         = UserIntFactory.CreateFromIdentity(_uow, User);
                NewThreadModel newThreadModel = new NewThreadModel(sender, user);
                newThreadModel.Prepare(createModel.Title, createModel.Text);

                int id = _uow.GetRepository <IMessageThreadRepository>().Add(newThreadModel.MessageThread);
                _uow.Commit();

                Message message = newThreadModel.Message;
                message.MessageThreadId = id;
                message.AuthorId        = newThreadModel.MessageThread.AuthorId;
                message.Status          = MessageStatus.New;

                int messageId = _uow.GetRepository <IMessageRepository>().Add(message);

                foreach (var messageReceiver in newThreadModel.MessageReceivers)
                {
                    messageReceiver.MessageId = messageId;
                    _uow.GetRepository <IMessageReceiverRepository>().Add(messageReceiver);
                }

                foreach (var messageThreadReceiver in newThreadModel.MessageThreadReceivers)
                {
                    messageThreadReceiver.MessageThreadId = id;
                    _uow.GetRepository <IMessageThreadReceiverRepository>().Add(messageThreadReceiver);
                }

                _uow.Commit();

                return(RedirectToAction("Index"));
            }

            return(View(createModel));
        }