Example #1
0
        public MatchModule(IRepository<Match> matchRepository, IRepository<Country> countryRepository)
            : base("/matches")
        {
            Get["/"] = parameters =>
                           {
                               var matches = matchRepository.FindAll();

                               var resource = matches.ToArray().Select(
                                   m => new MatchResource
                                            {
                                                Id = m.Id,
                                                Date = m.Date,
                                                Team1CountryId = m.Team1.Country.Id,
                                                Team2CountryId = m.Team2.Country.Id
                                            });

                               return Response.AsJson(resource);
                           };

            Post["/"] = parameters =>
                {
                    var resource = this.Bind<MatchResource>();

                    var country1 = countryRepository.GetById(resource.Team1CountryId);
                    if (country1 == null)
                        return HttpStatusCode.BadRequest;

                    var country2 = countryRepository.GetById(resource.Team2CountryId);
                    if (country2 == null)
                        return HttpStatusCode.BadRequest;

                    var match = Match.Create(
                        resource.Date,
                        country1,
                        country2);

                    matchRepository.Add(match);

                    return Response.AsRedirect("/matches/" + match.Id);
                };

            Get["/{id}"] = parameters =>
                {
                    var match = matchRepository.GetById((int)parameters.Id);
                    if (match == null)
                        return HttpStatusCode.NotFound;

                    return Response.AsJson(new
                        {
                            Date = match.Date,
                            Team1 = match.Team1,
                            Team2 = match.Team2
                        });
                };
        }
Example #2
0
        /// <summary>
        /// 从缓存中读取指定实体类型的所有数据。
        /// 如果缓存中不存在,或者缓存数据已经过期,则调用 GetByIdCore 方法获取数据,并把最终数据加入到缓存中。
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="id">The unique identifier.</param>
        /// <returns></returns>
        internal Entity CacheById(IRepository repository, object id)
        {
            Entity result = null;

            var sd = repository.EntityMeta.ClientCacheDefinition;
            if (sd != null)
            {
                var entityType = repository.EntityType;

                var className = entityType.Name;
                var key = GetCacheKey(id);

                //AggregateChecker 不支持序列化,所以这里只用内在缓存即可。
                result = this.Cache.Get(key, className) as Entity;

                if (result == null)
                {
                    result = repository.GetById(id);
                    if (result != null)
                    {
                        var policy = CreatePolicy(entityType, id);
                        this.Cache.Add(key, result, policy, className);
                    }
                }
            }

            return result;
        }
Example #3
0
        public static void CleanImageData(IRepository<Immagine> repository, string id)
        {
            if (string.IsNullOrEmpty(id)) return;

            RemoveFromDisk(id); //rimuove la cache da disco

            var immagine = repository.GetById(id);

            if (immagine == null) return;

            RemoveFromCloudRepository(immagine); //rimuove dalla repository di azure

            repository.DeleteOnSubmit(immagine); //rimuove dal db
        }
Example #4
0
        private static void NamePerson(IRepository<PersonDTO> personReadModel)
        {
            Console.Write("Enter person's Name: ");
            var name = Console.ReadLine();

            var id = Guid.NewGuid();
            var person = new Person(id);
            person.NameMe(name);

            var personDTO = personReadModel.GetById(id);
            Console.WriteLine("Person ID: " + personDTO.Id);
            Console.WriteLine("Person Name: " + personDTO.Name);
            Console.WriteLine("Person Was Renamed: " + personDTO.WasRenamed);
        }
Example #5
0
        private static void SampleMethod(IRepository<Product,int?> productRepository)
        {
            SessionProvider.RebuildSchema();

            //Create a Product
            var pNew = new Product { ProductName = "Canned Salmon" };
            productRepository.Save(pNew);

            //Get a Product
            var pGet = productRepository.GetById(pNew.ProductId);

            //Update a Product
            pGet.ProductName = "Canned Tuna";
            productRepository.Save(pGet);

            //Delete a Product
            productRepository.Delete(pNew);
        }
Example #6
0
        private static void NameAndRenamePerson(IRepository<PersonDTO> personReadModel)
        {
            Console.Write("Enter person's Name: ");
            var name = Console.ReadLine();

            var id = Guid.NewGuid();
            var person = new Person(id);
            person.NameMe(name);

            Console.Write("Enter person's Name: ");
            var renamedName = Console.ReadLine();
            person = EventStore.Instance.GetById<Person>(id); 
            //can also do this: 
            // person = MongoEventStore.Instance.GetById(id) as Person;
            person.NameMe(renamedName);

            var personDTO = personReadModel.GetById(id);
            Console.WriteLine("Person ID: " + personDTO.Id);
            Console.WriteLine("Person Name: " + personDTO.Name);
            Console.WriteLine("Person Was Renamed: " + personDTO.WasRenamed);
        }
Example #7
0
 void App_Startup(object sender, StartupEventArgs e)
 {
     IoCManager.Start();
   
     _userRepository = IoCManager.Kernel.Get<IRepository<User>>();
     //TO DO think how to improve quality of this solution.
     //Perhaps create some buffer 
     //like LocalStorage or Cookies for registration purpose.
     var currentUser = _userRepository.GetById(1);
     if (currentUser != null)
     {
         CurrentUser = currentUser.Name;
         MyBoooks myBoooks = new MyBoooks();
         myBoooks.Show();
     }
     else
     {
         Registration window = new Registration();
         window.Show();
     }
 }
Example #8
0
        public static Immagine UpdateSingleImage(IRepository<Immagine> immagineRepository,
            IUploadImageService uploadImageService, string oldId, ImmagineRawViewModel img, string idPadre,
            string tipoPadre)
        {
            if (img == null) return null;

            if (img.File == null)
            {   //aggiorno i POIPerc dell'immagine precedente
                if (string.IsNullOrEmpty(oldId)) return null;
                var oldImg = immagineRepository.GetById(oldId);

                if (oldImg == null) return null;

                oldImg.PoiPercX = img.PoiPercX;
                oldImg.PoiPercY = img.PoiPercY;
                return oldImg;
            }

            if (!string.IsNullOrEmpty(oldId))
                CleanImageData(immagineRepository, oldId);

            //inserisco la nuova immagine
            var images = uploadImageService.UploadImages(new List<HttpPostedFileBase>{img.File}, idPadre,
                tipoPadre, true);

            var result = images != null && images.Any() ? images.First() : null;
            if (result == null) return null;

            result.PoiPercX = img.PoiPercX;
            result.PoiPercY = img.PoiPercY;

            return result;
        }
        public JsonResult Edit(BonusDto bonusDto)
        {
            if (bonusDto == null)
                throw new ArgumentNullException("bonusDto can not be null in controller Edit");

            Employee employee = null;
            BonusAggregate bonus = null;
            using (var dbContext = new DatabaseContext())
            {
                if (bonusDto.EmployeeId != 0)
                {
                    var employeeRepository = new EmployeesRepository(dbContext);
                    employee = employeeRepository.GetById(bonusDto.EmployeeId);
                }

                BonusesRepository = new BonusesRepository(dbContext);
                bonus = BonusesRepository.GetById(bonusDto.BonusId);
                bonus.Comment = bonusDto.Comment;
                bonus.Amount = bonusDto.Amount;
                bonus.Date = bonusDto.Date;
                bonus.IsActive = bonusDto.IsActive;

                if (employee != null &&
                    employee.EmployeeId != bonus.EmployeeId)
                {
                    bonus.Employee = employee;
                }

                BonusesRepository.Save(bonus);
            }

            return Json(bonus);
        }
        /// <summary>
        /// Gets the employee by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>Employee.</returns>
        private Employee GetEmployeeById(int id = 1)
        {
            Employee employee;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                employeeRepository = new EmployeesRepository(dbContext);
                employee = employeeRepository.GetById(id);
            }

            return employee;
        }
        public void Save_BonusesList_2BonusesUpdated()
        {
            IList<BonusAggregate> bonusesToUpdate;
            IList<BonusAggregate> updatedBonuses = new List<BonusAggregate>();
            var bonusesIds = new int[2];

            string newComment = "comment on " + DateTime.Now;
            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusesToUpdate = bonusRepository.FindAll().Take(2).ToList();
                bonusesToUpdate[0].Comment = newComment;
                bonusesToUpdate[1].Comment = newComment;

                bonusesIds[0] = bonusesToUpdate[0].BonusId;
                bonusesIds[1] = bonusesToUpdate[1].BonusId;
                bonusRepository.Save(bonusesToUpdate);
            }

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                updatedBonuses.Add(bonusRepository.GetById(bonusesIds[0]));
                updatedBonuses.Add(bonusRepository.GetById(bonusesIds[1]));
            }

            Assert.AreEqual(updatedBonuses[0].Comment, newComment);
            Assert.AreEqual(updatedBonuses[1].Comment, newComment);
        }
 protected override void Before_each()
 {
     base.Before_each();
     _repo = new Repository<AssaultItem>(CreateSessionFactory());
     _itemToDelete = _repo.GetById(1);
 }
        public static UserListViewModel Create(IUserBLL userBLL, IRepository<Project> projectRepository, int? filterProjectId)
        {
            var users = filterProjectId == null
                            ? userBLL.GetAllUsers()
                            : userBLL.GetAllUsers().Where(x => x.Projects.Contains(projectRepository.GetById(filterProjectId.Value)));

            var viewModel = new UserListViewModel
                                {
                                    Users = users.ToList(),
                                    Projects = userBLL.GetAllProjectsByUser(projectRepository).ToList(),
                                    SelectedFilterProjectId = filterProjectId
                                };

            return viewModel;
        }
 private void LoadReadingList()
 {
     _userRepository = IoCManager.Kernel.Get<IRepository<User>>();
     _user = _userRepository.GetById(1);
     _readinglistContent = _user.ReadingList.RadingList;
 }
Example #15
0
        // TODO: let's do content negotiation ourselves unless there is a nice
        // way to do it in Nancy like there seems to be for most things
        public CountryModule(IRepository<Domain.Country> countryRepository, CountryFactory countryFactory)
            : base("/countries")
        {
            Get["/"] = parameters =>
                {
                    var countryResources = countryRepository.FindAll()
                        .Select(c => new Country {id = c.Id.ToString(), Name = c.Name});

                    var resource = countryResources.ToArray();

                    return Response.AsJson(resource);
                };

            Get["/{id}"] = parameters =>
                {
                    var country = countryRepository.GetById((int)parameters.id);
                    if (country == null)
                        return HttpStatusCode.NotFound;

                    var resource = new Country {id = country.Id.ToString(), Name = country.Name};

                    return Response.AsJson(resource);
                };

            Post["/"] = parameters =>
                {
                    var countryResource = this.Bind<Country>(blacklistedProperties:"id");

                    var validationResult = this.Validate(countryResource);
                    if (! validationResult.IsValid)
                    {
                        var response = Response.AsJson(validationResult.Errors);
                        response.StatusCode = HttpStatusCode.BadRequest;
                        return response;
                    }

                    var country = countryFactory.CreateCountry(countryResource.Name);
                    countryRepository.Add(country);

                    return Response.AsRedirect("/countries/"+country.Id);
                };

            Put["/{id}"] = parameters =>
                {
                    var country = countryRepository.GetById((int) parameters.id);
                    if (country == null)
                        return HttpStatusCode.NotFound; // this correct for a put? should probably be some error

                    // we don't actually support updates to countries!
                    //country.Name = parameters.Name;

                    return Response.AsRedirect("/countries/" + country.Id);
                };

            Delete["/{id}"] = parameters =>
                {
                    var country = countryRepository.GetById((int)parameters.id);
                    if (country == null)
                        return HttpStatusCode.NotFound; // this correct for a put? should probably be some error

                    countryRepository.Remove(country);

                    return HttpStatusCode.OK;
                };
        }
        public void GetById_NoParams_Bonus()
        {
            BonusAggregate bonusAggregate;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusAggregate = bonusRepository.GetById(1);
            }

            Assert.AreEqual(1, bonusAggregate.BonusId);
        }
Example #17
0
 public Stock(IRepository<Product, int> repository, int id)
 {
     this.repository = repository;
     this.product = repository.GetById(id);
 }
 protected override void Before_each()
 {
     base.Before_each();
     _repo = new Repository<AssaultItem>(CreateSessionFactory());
     _item = _repo.GetById(2);
     _item.Description = _descUpdate;
 }