public async Task <FieldOfInterestView> RetrieveOriginalValuesAsync(FieldOfInterestView field)
        {
            var entityField = new FieldOfInterest()
            {
                FieldOfInterestId = field.FieldOfInterestId
            };
            var entityFieldDescription = new FieldOfInterestDescription()
            {
                FieldOfInterestId = field.FieldOfInterestId, LanguageId = field.LanguageId
            };

            var originalValues = await _context.Entry(entityField).GetDatabaseValuesAsync();

            if (originalValues == null)
            {
                return(null);
            }

            var originalField = (FieldOfInterest)originalValues.ToObject();

            // retrieving the description dont work on detached / not-tracked entities (like originalField) when using EF lazy loading
            // originalField.Descriptions.Add((FieldOfInterestDescription)_context.Entry(entityFieldDescription).GetDatabaseValues().ToObject());
            // return MapToFieldOfInterestView(originalField);

            var originalValuesDescription = await _context.Entry(entityFieldDescription).GetDatabaseValuesAsync();

            FieldOfInterestDescription originalFieldDescription;

            if (originalValuesDescription == null)
            {
                originalFieldDescription = new FieldOfInterestDescription();
            }
            else
            {
                originalFieldDescription = (FieldOfInterestDescription)originalValuesDescription.ToObject();
            }

            return(new FieldOfInterestView
            {
                FieldOfInterestId = field.FieldOfInterestId,
                LanguageId = field.LanguageId,
                Description = originalFieldDescription.Description,
                Created = originalField.Created,
                CreatedBy = originalField.CreatedBy,
                Updated = originalField.Updated,
                UpdatedBy = originalField.UpdatedBy,
                RowVersion = originalField.RowVersion
            });
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "Id,Name,CategoryId")] FieldOfInterestViewModel fieldOfInterest)
        {
            FieldOfInterest foi = new FieldOfInterest {
                Name = fieldOfInterest.Name, Category = db.Categories.Where(x => x.Id == fieldOfInterest.CategoryId).FirstOrDefault()
            };

            if (ModelState.IsValid)
            {
                db.Fields.Add(foi);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(fieldOfInterest));
        }
        private FieldOfInterestView MapToFieldOfInterestView(FieldOfInterest field)
        {
            string languageId;
            string description = DescriptionHelper.GetDescription(field.Descriptions, out languageId);

            return(new FieldOfInterestView
            {
                FieldOfInterestId = field.FieldOfInterestId,
                LanguageId = languageId,
                Description = description,
                Created = field.Created,
                CreatedBy = field.CreatedBy,
                Updated = field.Updated,
                UpdatedBy = field.UpdatedBy,
                RowVersion = field.RowVersion
            });
        }
        public ActionResult Create(InterestViewModel interest)
        {
            if (ModelState.IsValid)
            {
                GeneralUser     user    = db.Users.FirstOrDefault(u => u.Email == User.Identity.Name);
                FieldOfInterest foi     = db.Fields.FirstOrDefault(f => f.Id == interest.FOIId);
                UserFOI         userFOI = new UserFOI()
                {
                    User  = user,
                    Foi   = foi,
                    Level = interest.Level
                };
                db.UserFOIs.Add(userFOI);
                db.SaveChanges();
                return(RedirectToAction("Index", user is StudentUser ? "student" : "providers", null));
            }

            return(View(interest));
        }
        public IHttpActionResult AddProjectInterest(ProjInterestViewModel projModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Project         proj = db.Projects.FirstOrDefault(p => p.Id == projModel.ProjId);
            FieldOfInterest foi  = db.Fields.FirstOrDefault(f => f.Id == projModel.FOIId);

            ProjectFOI projFoi = new ProjectFOI {
                Project = proj, Foi = foi
            };

            db.ProjectFOIs.Add(projFoi);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = projFoi.Id }, projFoi));
        }
        public IHttpActionResult AddUserInterest(InterestViewModel interest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            GeneralUser     user    = db.Users.FirstOrDefault(u => u.Email == User.Identity.Name);
            FieldOfInterest foi     = db.Fields.FirstOrDefault(f => f.Id == interest.FOIId);
            UserFOI         userFOI = new UserFOI()
            {
                User  = user,
                Foi   = foi,
                Level = interest.Level
            };

            db.UserFOIs.Add(userFOI);
            db.SaveChanges();

            return(Ok());
        }
        // Can be a good idee to have a generic seeder method to if there are lots of database operations to check.
        // Makes the code a bit more readable, but on the downside is changing the seeded function should mean a
        // rerun all the tests using the seeder function and perhaps updating some.
        private static async Task SeedContextWithFieldsOfInterest(DatabaseContext context)
        {
            var field = new FieldOfInterest()
            {
                FieldOfInterestId = "01", Created = DateTime.Now, CreatedBy = "Anonymous", Updated = DateTime.Now, UpdatedBy = ""
            };

            field.Descriptions.Add(new FieldOfInterestDescription()
            {
                FieldOfInterestId = "01", LanguageId = "NL", Description = "Field 01/NL"
            });
            field.Descriptions.Add(new FieldOfInterestDescription()
            {
                FieldOfInterestId = "01", LanguageId = "EN", Description = "Field 01/EN"
            });
            context.Add(field);

            field = new FieldOfInterest()
            {
                FieldOfInterestId = "02", Created = DateTime.Now, CreatedBy = "Anonymous", Updated = DateTime.Now, UpdatedBy = ""
            };
            field.Descriptions.Add(new FieldOfInterestDescription()
            {
                FieldOfInterestId = "02", LanguageId = "NL", Description = "Field 02/NL"
            });
            field.Descriptions.Add(new FieldOfInterestDescription()
            {
                FieldOfInterestId = "02", LanguageId = "EN", Description = "Field 02/EN"
            });
            field.Descriptions.Add(new FieldOfInterestDescription()
            {
                FieldOfInterestId = "02", LanguageId = "DE", Description = "Field 02/DE"
            });
            context.Add(field);

            await context.SaveChangesAsync();

            // Checks a redundant, since hope to assume EF is well tested
            field = context.Find <FieldOfInterest>("01");
            Assert.AreEqual(context.Entry(field).State, EntityState.Unchanged, "The field of interest 01 should be marked unchanged");

            var fieldDescription = context.Find <FieldOfInterestDescription>("01", "NL");

            Assert.AreEqual(context.Entry(fieldDescription).State, EntityState.Unchanged, "The field of interest 01 description NL should be marked unchanged");

            fieldDescription = context.Find <FieldOfInterestDescription>("01", "EN");
            Assert.AreEqual(context.Entry(fieldDescription).State, EntityState.Unchanged, "The field of interest 01 description EN should be marked unchanged");

            fieldDescription = context.Find <FieldOfInterestDescription>("01", "DE");
            Assert.IsNull(fieldDescription, "The field of interest 01 description DE should not exist");

            field = context.Find <FieldOfInterest>("02");
            Assert.AreEqual(context.Entry(field).State, EntityState.Unchanged, "The field of interest 02 should be marked unchanged");

            fieldDescription = context.Find <FieldOfInterestDescription>("02", "NL");
            Assert.AreEqual(context.Entry(fieldDescription).State, EntityState.Unchanged, "The field of interest 02 description NL should be marked unchanged");

            fieldDescription = context.Find <FieldOfInterestDescription>("02", "EN");
            Assert.AreEqual(context.Entry(fieldDescription).State, EntityState.Unchanged, "The field of interest 02 description EN should be marked unchanged");

            fieldDescription = context.Find <FieldOfInterestDescription>("02", "DE");
            Assert.AreEqual(context.Entry(fieldDescription).State, EntityState.Unchanged, "The field of interest 02 description DE should be marked unchanged");
        }