Ejemplo n.º 1
0
 public IHttpActionResult Get()
 {
     using (var db = new MyReadStore())
     {
         return(Ok(db.SomeItems.ToList()));
     }
 }
Ejemplo n.º 2
0
        public void Handle(UpdateItemCommand command)
        {
            try
            {
                Thread.Sleep(new Random().Next(500, 4000));

                if (command.Id == Guid.Empty)
                {
                    throw new ArgumentNullException("command.Id");
                }
                if (string.IsNullOrEmpty(command.NewName))
                {
                    throw new ArgumentNullException("command.Name");
                }

                Console.WriteLine("Got command: {0}, Id: {1}, NewName: {2}", command.GetType().Name, command.Id, command.NewName);

                using (var db = new MyReadStore())
                {
                    var item = db.SomeItems.Where(x => x.Id == command.Id).FirstOrDefault();
                    if (item == null)
                    {
                        throw new Exception("Item with id " + command.Id.ToString() + " does not exist.");
                    }
                    item.Name     = command.NewName;
                    item.IsActive = command.IsActive;
                    db.SaveChanges();
                }

                _publisher.Publish(new ItemUpdatedEvent()
                {
                    Id = command.Id, Name = command.NewName
                });
                _publisher.Publish(new ClientNotification()
                {
                    Success  = true,
                    Message  = string.Format("The item '{0}' was updated.", command.NewName),
                    ClientId = command.ClientId
                });
            }

            catch (Exception ex)
            {
                Console.WriteLine("Failed to execute command: {0}, Message: {1}", command.GetType().Name, ex.Message);
                _publisher.Publish(new ClientNotification()
                {
                    Success  = false,
                    Message  = string.Format("Could not update the item. Errormessage: ", ex.Message),
                    ClientId = command.ClientId
                });
            }
        }
Ejemplo n.º 3
0
        public void Handle(AddItemCommand command)
        {
            try
            {
                Thread.Sleep(new Random().Next(500, 4000));

                if (command.Id == Guid.Empty)
                {
                    throw new ArgumentNullException("command.Id");
                }
                if (string.IsNullOrEmpty(command.Name))
                {
                    throw new ArgumentNullException("command.Name");
                }

                Console.WriteLine("Got command: {0}, Id: {1}, Name: {2}", command.GetType().Name, command.Id, command.Name);

                using (var db = new MyReadStore())
                {
                    if (db.SomeItems.Where(x => x.Id == command.Id).Any())
                    {
                        throw new Exception("Item does already exists");
                    }
                    db.SomeItems.Add(new SomeItem()
                    {
                        Id = command.Id, Name = command.Name, IsActive = false
                    });
                    db.SaveChanges();
                }
                _publisher.Publish(new ItemAddedEvent()
                {
                    Id = command.Id, Name = command.Name
                });
                _publisher.Publish(new ClientNotification()
                {
                    Success  = true,
                    Message  = string.Format("The item '{0}' was added", command.Name),
                    ClientId = command.ClientId
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to execute command: {0}, Message: {1}", command.GetType().Name, ex.Message);
                _publisher.Publish(new ClientNotification()
                {
                    Success  = false,
                    Message  = string.Format("Could not add the item. Errormessage: ", ex.Message),
                    ClientId = command.ClientId
                });
            }
        }