예제 #1
0
        public MPost Insert(PostInsertRequest request)
        {
            Post newPost = new Post
            {
                CategoryId   = request.Category.Id,
                CreationDate = request.CreationDate,
                Summary      = request.Summary,
                Title        = request.Title,
                Image        = request.Image,
                UserId       = request.PostOwner.Id
            };

            ctx.Posts.Add(newPost);
            ctx.SaveChanges();

            foreach (var item in request.Tags)
            {
                PostTag pt = new PostTag {
                    PostId = newPost.Id, TagId = item.Id
                };
                ctx.PostTags.Add(pt);
            }

            ctx.SaveChanges();

            return(_mapper.Map <MPost>(newPost));
        }
예제 #2
0
        public int WriteData(Data data)
        {
            try
            {
                DB.City city   = _ctx.Cities.FirstOrDefault(n => n.Id.Equals(data.Idx));
                AQData  aQData = new AQData();

                aQData.PM10        = data.Iaqi.pm10.V;
                aQData.PM25        = data.Iaqi.pm25.V;
                aQData.Temperature = data.Iaqi.t.V;
                aQData.Date        = DateTime.Parse(data.Time.S);
                aQData.Humidity    = data.Iaqi.h.V;


                if (city != null)
                {
                    aQData.CityId = city.Id;
                }
                else
                {
                    DB.City newCity = new DB.City();
                    newCity.Name  = GetShortName(data.City.Name);
                    newCity.Id    = data.Idx;
                    aQData.CityId = data.Idx;
                    _ctx.Cities.Add(newCity);
                }

                _ctx.AQDatas.Add(aQData);
                return(_ctx.SaveChanges());
            }
            catch (Exception ex)
            {
                return(0);
            }
        }
예제 #3
0
        public MSeason Insert(SeasonInsertRequest request)
        {
            var episodes      = request.Episodes.OrderBy(x => x.AirDate).ToList();
            var ordinalNumber = ctx.Seasons.Count() + 1;
            var season        = new Season
            {
                NoOfEpisodes  = episodes.Count(),
                OrdinalNumber = ordinalNumber,
                PremiereDate  = (DateTime)episodes.First().AirDate,
                Summary       = "test test",
                Episodes      = new List <Episode>()
            };

            for (int i = 0; i <= episodes.Count() - 1; i++)
            {
                episodes[i].SeasonEpisodeNumber = i + 1;
            }


            ctx.Seasons.Add(season);

            season.Episodes = _mapper.Map <List <Episode> >(episodes);


            ctx.SaveChanges();

            return(_mapper.Map <MSeason>(season));
        }
예제 #4
0
        public MFamily Insert(FamilyInsertRequest request)
        {
            var family = _mapper.Map <Family>(request);

            _ctx.Families.Add(family);
            _ctx.SaveChanges();
            return(_mapper.Map <MFamily>(family));
        }
        public MCharacter Insert(CharacterInsert request)
        {
            var NewCharacter = new Character
            {
                Biography  = request.Biography,
                BirthDate  = request.BirthDate,
                FirstName  = request.FirstName,
                LastName   = request.LastName,
                Occupation = request.Occupation
            };

            _ctx.Characters.Add(NewCharacter);

            NewCharacter.Family             = _mapper.Map <Family>(request.Family);
            NewCharacter.CharacterMediaFile = _mapper.Map <CharacterMediaFile>(request.MediaFile);

            _ctx.SaveChanges();

            return(_mapper.Map <MCharacter>(NewCharacter));
        }
예제 #6
0
        public void TestAddCreditCard()
        {
            var cc = AppCtx.CreditCards.Create();

            cc.CardNumber = "xxxx-xxxx-xxxx-xxxx";
            cc.CCV        = 128;
            cc.ExpMonth   = 4;
            cc.ExpYear    = 2022;
            cc.Network    = new CreditCardNetwork
            {
                Name = "Visa"
            };
            AppCtx.CreditCards.Add(cc);
            AppCtx.SaveChanges();
            Assert.AreEqual(1, AppCtx.CreditCards.Count(c => c.CardNumber == "xxxx-xxxx-xxxx-xxxx"));
            Assert.AreEqual(1, AppCtx.CreditCardNetworks.Count(n => n.Name == "Visa"));
        }
예제 #7
0
        public MEpisode Delete(int id)
        {
            var result = ctx.Episodes.Include(x => x.MediaFile).Include(x => x.Season).Where(x => x.Id == id).FirstOrDefault();

            if (result.Season != null)
            {
                result.Season.Episodes.Remove(result);

                if (--result.Season.NoOfEpisodes == 0)
                {
                    ctx.Seasons.Remove(result.Season);
                }
            }

            ctx.Episodes.Remove(result);
            ctx.MediaFiles.Remove(result.MediaFile);

            ctx.SaveChanges();

            return(_mapper.Map <MEpisode>(result));
        }
예제 #8
0
        public MUser InsertUser(UserInsertRequest request)
        {
            var user = _mapper.Map <User>(request);

            user.PasswordSalt = HashHelper.GenerateSalt();
            user.PasswordHash = HashHelper.GenerateHash(user.PasswordSalt, request.Password);

            _ctx.Users.Add(user);

            _ctx.SaveChanges();

            foreach (var items in request.RolesId)
            {
                var userRole = new UserRole {
                    RoleId = items, UserId = user.Id
                };
                _ctx.UserRoles.Add(userRole);
            }

            _ctx.SaveChanges();

            return(_mapper.Map <MUser>(user));
        }
예제 #9
0
        public void Seed(string fileName, AppCtx db)
        {
            try
            {
                IList <JsonEmployee> employees = null;
                employees = JsonConvert.DeserializeObject <IList <JsonEmployee> >(new StreamReader(fileName).ReadToEnd());

                Dictionary <string, Team> mapOfTeams = new Dictionary <string, Team>(new StringKeyComparer());
                Dictionary <string, Role> mapOfRoles = new Dictionary <string, Role>(new StringKeyComparer());



                List <Employee> dbEmployees = new List <Employee>();

                foreach (var je in employees)
                {
                    var dbEmployee = new Employee()
                    {
                        Age       = je.Age,
                        FirstName = je.Name,
                        ManagerId = je.ManagerId,
                        Surname   = je.SurName
                    };

                    AddIfNotExistsInDictionary <Role>(je.Role, mapOfRoles);

                    je.Teams.ForEach(t => AddIfNotExistsInDictionary(t, mapOfTeams));

                    dbEmployee.Role  = mapOfRoles[je.Role.ToLower()];
                    dbEmployee.Teams = je.Teams
                                       .Select(t => t.ToLower())
                                       .Distinct()
                                       .Select(t => new EmployeeTeam()
                    {
                        Employee = dbEmployee, Team = mapOfTeams[t]
                    })
                                       .ToList();

                    dbEmployees.Add(dbEmployee);
                }

                db.Database.OpenConnection();
                db.Database.ExecuteSqlCommand("DBCC checkident ('dbo.Employee', reseed, 0)");

                int count = 0;
                foreach (var dbEm in dbEmployees)
                {
                    db.Employees.Add(dbEm);
                    count++;

                    if (count == 10)
                    {
                        db.SaveChanges();
                        count = 0;
                    }
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Database.CloseConnection();
            }
        }