Example #1
0
        public void Should_Get_Related_Sessions()
        {
            Person person = new Person
            {
                Name      = "Tugberk",
                BirthDate = new DateTime(2008, 05, 23),
                Sessions  = new Collection <Session>()
            };

            using (ConfContext ctx = new ConfContext())
            {
                using (ctx.Database.BeginTransaction())
                {
                    person.Sessions.Add(new Session {
                        Name = "Ses 1"
                    });
                    person.Sessions.Add(new Session {
                        Name = "Ses 2"
                    });
                    person.Sessions.Add(new Session {
                        Name = "Ses 3"
                    });

                    ctx.People.Add(person);
                    ctx.SaveChanges();

                    Person retrievedPerson = ctx.People.Include(personEntity => personEntity.Sessions).FirstOrDefault();

                    Assert.NotNull(retrievedPerson);
                    Assert.Equal(3, retrievedPerson.Sessions.Count());
                }
            }
        }
Example #2
0
        public void Should_Save_LastUpdatedOn_On_Modified_Save()
        {
            Person person = new Person
            {
                Name      = "Tugberk",
                BirthDate = new DateTime(2008, 05, 23)
            };

            using (ConfContext ctx = new ConfContext())
            {
                using (new TransactionScope())
                {
                    ctx.Entry(person).State = EntityState.Added;
                    ctx.SaveChanges();

                    DateTimeOffset firstLastModifiedDate = person.LastUpdatedOn;

                    person.Name             = "Tugberk2";
                    ctx.Entry(person).State = EntityState.Modified;
                    ctx.SaveChanges();

                    Assert.True(person.LastUpdatedOn > firstLastModifiedDate);
                }
            }
        }
Example #3
0
 public IEnumerable <PersonDto> GetPeople()
 {
     using (ConfContext _confContext = new ConfContext())
     {
         Person[] people = _confContext.People.ToArray();
         return(Mapper.Map <IEnumerable <Person>, IEnumerable <PersonDto> >(people));
     }
 }
Example #4
0
        public HttpResponseMessage PutPerson(int id, PersonRequestModel requestModel)
        {
            // Change tracker has a deep knowladge on the Timestamp property and
            // it sends the first retrieved Timestamp no matter what the latter value is.
            // That's why we are doing the operations with two seperate context objects.
            // ref: http://stackoverflow.com/questions/4402586/optimisticconcurrencyexception-does-not-work-in-entity-framework-in-certain-situ
            // http://stackoverflow.com/questions/13581473/why-does-the-objectstatemanager-property-not-exist-in-my-db-context

            using (ConfContext _confContext = new ConfContext())
            {
                Person existingPerson = _confContext.People.FirstOrDefault(personEntity => personEntity.Id == id);
                if (existingPerson == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.NotFound));
                }

                string ifMatchHeader;
                if (Request.Headers.IfMatch.TryGetETag(out ifMatchHeader))
                {
                    //ObjectStateManager objectStateManager = ((IObjectContextAdapter)_confContext).ObjectContext.ObjectStateManager;
                    //objectStateManager.ChangeObjectState(existingPerson, EntityState.Detached);
                    _confContext.Entry(existingPerson).State = EntityState.Detached;

                    ifMatchHeader = ifMatchHeader.Trim('"');
                    byte[] timestamp = Convert.FromBase64String(ifMatchHeader);
                    existingPerson.Timestamp = timestamp;
                    Person person = Mapper.Map <PersonRequestModel, Person>(requestModel, existingPerson);

                    try
                    {
                        _confContext.Entry(person).State = EntityState.Modified;
                        _confContext.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        const string message = "The record you attempted to edit was modified by another user after you got the original value.";
                        return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, message));
                    }
                    catch (DataException)
                    {
                        //Log the error (add a variable name after Exception)
                        const string message = "Unable to save changes. Try again, and if the problem persists contact your system administrator.";
                        return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
                    }

                    return(Request.CreateResponse(HttpStatusCode.OK, Mapper.Map <Person, PersonDto>(person)));
                }

                return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "If-Match header needs to be provided in order to update the entity."));
            }
        }
Example #5
0
        public HttpResponseMessage PostPerson(PersonRequestModel requestModel)
        {
            using (ConfContext _confContext = new ConfContext())
            {
                Person person = Mapper.Map <PersonRequestModel, Person>(requestModel);
                _confContext.People.Add(person);
                int result = _confContext.SaveChanges();
                if (result > 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, Mapper.Map <Person, PersonDto>(person)));
                }

                return(Request.CreateResponse(HttpStatusCode.Conflict));
            }
        }
Example #6
0
        public HttpResponseMessage GetPerson(int id)
        {
            using (ConfContext _confContext = new ConfContext())
            {
                Person person = _confContext.People.FirstOrDefault(personEntity => personEntity.Id == id);
                if (person == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.NotFound));
                }

                PersonDto           personDto = Mapper.Map <Person, PersonDto>(person);
                HttpResponseMessage response  = Request.CreateResponse(HttpStatusCode.OK, personDto);
                response.Headers.ETag = new EntityTagHeaderValue(string.Concat("\"", Convert.ToBase64String(person.Timestamp, Base64FormattingOptions.None), "\""));

                return(response);
            }
        }
Example #7
0
        public void Should_Save_LastUpdatedOn_On_First_Save()
        {
            Person person = new Person
            {
                Name      = "Tugberk",
                BirthDate = new DateTime(2008, 05, 23)
            };

            using (ConfContext ctx = new ConfContext())
            {
                using (new TransactionScope())
                {
                    ctx.Entry(person).State = EntityState.Added;
                    ctx.SaveChanges();

                    Assert.NotEqual(person.LastUpdatedOn, default(DateTimeOffset));
                }
            }
        }
Example #8
0
 public TalksController(ConfContext context, IMapper mapper)
 {
     this.context = context;
     this.mapper  = mapper;
 }
 public FileRepository(ConfContext confContext) : base(confContext)
 {
 }
Example #10
0
 public LectureRepository(ConfContext confContext) : base(confContext)
 {
 }
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            ConfContext ctx = (ConfContext)request.GetDependencyScope().GetService(typeof(ConfContext));

            return(base.SendAsync(request, cancellationToken));
        }
 public FileNotificationRepository(ConfContext confContext) : base(confContext)
 {
 }
 public InfoPageRepository(ConfContext confContext) : base(confContext)
 {
 }
 public ApplicationRepository(ConfContext confContext) : base(confContext)
 {
 }
Example #15
0
 public SpeakerRepository(ConfContext context)
 {
     this.context = context;
 }
Example #16
0
 public BaseRepository(ConfContext confContext)
 {
     _context = confContext;
 }
Example #17
0
 public WorkshopRepository(ConfContext context)
 {
     this.context = context;
 }
 public ConferenceRepository(ConfContext confContext) : base(confContext)
 {
 }
Example #19
0
 public TalkRepository(ConfContext context)
 {
     this.context = context;
 }
 public MessageNotificationRepository(ConfContext confContext) : base(confContext)
 {
 }
 public ConfController(ConfContext confContext, Func <Owned <ConfContext> > confContextFactory)
 {
     _confContext        = confContext;
     _confContextFactory = confContextFactory;
 }
Example #22
0
 public UserRepository(ConfContext confContext) : base(confContext)
 {
 }
 public SectionExpertRepository(ConfContext confContext) : base(confContext)
 {
 }
 public MessageRepository(ConfContext confContext) : base(confContext)
 {
 }