Beispiel #1
0
        /// <summary>
        /// тесты для функции сервиса AddRelative
        /// </summary>
        public static void TestAddRelative()
        {
            dataFormat = "json";
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;

            string operationUrl = "http://localhost:8732/Design_Time_Addresses/RESTService/AddRelative";
            string passportNumber = "1111654321";
            Person person = new Person
            {
                Address = "",
                DateOfBirth = null,
                FirstName = "Julia",
                PassportNumber = "1114654321",
                PersonID = 0,
                SecondName = "Glazacheva",
                Sex = "female",
                ThirdName = "Igorevna"
            };
            string state = "sister";
            Relative relative = new Relative(person, state);

            
            #region Create params
            string relativeStr = "";
            relativeStr = "{\"relative\":" + JsonConvert.SerializeObject(relative, settings) + "}";
            Console.WriteLine(relativeStr);
            byte[] data = Encoding.GetEncoding(1251).GetBytes(relativeStr);          
            #endregion

            int i = 0;
            int[] personesCount = new int[2];
            int[] relationshipCount = new int[2];
            Stream stream = null;
            WebRequest req = null;
            WebResponse resp = null;
            StreamReader sr = null;
            while (i < 2)
            {
                #region Create Request
                req = WebRequest.Create(operationUrl + "?passportNumber=" + passportNumber + "&mode=" + mode);
                req.Method = "POST";
                req.Timeout = 12000;
                req.ContentType = "application/" + dataFormat;
                req.ContentLength = data.Length;
                #endregion

                #region Send Request
                Console.WriteLine("iter - " + i);
                stream = req.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                #endregion

                #region Get Response
                resp = req.GetResponse();
                stream = resp.GetResponseStream();
                sr = new System.IO.StreamReader(stream);
                string s = sr.ReadToEnd();
                Console.WriteLine(s);
                //Console.WriteLine(s.Length);
                #endregion

                Assert.IsTrue(s == "\"relative added successfully\"");

                var sqlP = from p in persones.GetContent() select p.PersonID;
                var sqlR = from r in relationships.GetContent() select r.RelationshipID;
                personesCount[i] = sqlP.Count();
                relationshipCount[i] = sqlR.Count();
                i++;
            }

            Assert.IsTrue(personesCount[0] == personesCount[1]);
            Assert.IsTrue(relationshipCount[0] == relationshipCount[1]);
        }
 public string AddRelative(string pasportNumber, Relative relative, string mode)
 {
     try
     {
         string message = "relative added successfully";
         int personID = 0;
         int relativeID = 0;
         Relationship relationship;
         try
         {
             //проверяем есть ли уже родственник в таблице Persones, если нет - добавляем
             var tryRelative = from p in Persones.GetContent() where p.PassportNumber == relative.Person.PassportNumber select p.PersonID;
             if (tryRelative.Count() == 0)
             {
                 if ((relative.Person.DateOfBirth != null) && (relative.Person.DateOfBirth.Value.Year < 1900)) relative.Person.DateOfBirth = null;
                 Persones.Insert(relative.Person);
                 Context.SubmitChanges();
                 relativeID = relative.Person.PersonID;
             }
             else
             {
                 relativeID = tryRelative.First();
             }
             var tryPerson = (from p in Persones.GetContent() where p.PassportNumber == pasportNumber select p).FirstOrDefault();
             if (tryPerson != null) { personID = tryPerson.PersonID; }
             else { return "there's no person in DB with passport number " + pasportNumber; }
             //запрашиваем список родственников персоны
             ToDispose = false;
             List<Relative> relatives = GetRelativesList(pasportNumber, null);
             ToDispose = true;
             //проверяем существует ли уже между персоной и родственником отношение, если нет - создаем
             if (relatives.Where(rel => rel.Person.PersonID == relativeID).Count() == 0)
             {
                 relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = personID, State = relative.RelationshipState };
                 Relationships.Insert(relationship);
             }
             if ((relatives.Count != 0) && (mode == "auto"))
             {
                 foreach (var rel in relatives)
                 {
                     relationship = null;
                     string newState = "";
                     //проверяем есть ли в базе отношение между добавленным родственником и существующим, если нет - 
                     //пытаемся определить тип отношения и добавить
                     var result = from r in Relationships.GetContent()
                                  where (((r.FirstPersonID == relativeID) && (r.SecondPersonID == rel.Person.PersonID)) ||
                                        ((r.FirstPersonID == rel.Person.PersonID) && (r.SecondPersonID == relativeID)))
                                  select r.RelationshipID;
                     if ((result.Count() == 0) && (relativeID != rel.Person.PersonID))
                     {
                         switch (relative.RelationshipState)
                         {
                             //определение типа родственного отношения, читается как:
                             //если новый родственник приходится мне тем-то(case) и у меня есть тот-то(if),
                             //то новый родственник приходится старому тем-то(newState)
                             #region Relationship logic
                             case ("son"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "brother";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "father") || (rel.RelationshipState == "mother"))
                                 {
                                     newState = "grandson";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "nephew";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "wife") || (rel.RelationshipState == "husband"))
                                 {
                                     newState = "son";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("daughter"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "sister";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "father") || (rel.RelationshipState == "mother"))
                                 {
                                     newState = "granddaughter";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "niece";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "wife") || (rel.RelationshipState == "husband"))
                                 {
                                     newState = "daughter";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("father"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "grandfather";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if (rel.RelationshipState == "mother")
                                 {
                                     newState = "husband";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "father";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("mother"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "grandmother";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if (rel.RelationshipState == "father")
                                 {
                                     newState = "wife";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "mother";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("sister"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "aunt";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "father") || (rel.RelationshipState == "mother"))
                                 {
                                     newState = "daughter";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "sister";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "grandfather") || (rel.RelationshipState == "grandmother"))
                                 {
                                     newState = "granddaughter";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "aunt") || (rel.RelationshipState == "uncle"))
                                 {
                                     newState = "niece";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "niece") || (rel.RelationshipState == "nephew"))
                                 {
                                     newState = "aunt";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("brother"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "uncle";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "father") || (rel.RelationshipState == "mother"))
                                 {
                                     newState = "son";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "brother";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "grandfather") || (rel.RelationshipState == "grandmother"))
                                 {
                                     newState = "grandson";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "aunt") || (rel.RelationshipState == "uncle"))
                                 {
                                     newState = "nephew";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 else if ((rel.RelationshipState == "niece") || (rel.RelationshipState == "nephew"))
                                 {
                                     newState = "uncle";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("wife"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "mother";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("husband"):
                                 if ((rel.RelationshipState == "son") || (rel.RelationshipState == "daughter"))
                                 {
                                     newState = "father";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("aunt"):
                                 if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "aunt";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("uncle"):
                                 if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "uncle";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("grandfather"):
                                 if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "grandfather";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("grandmother"):
                                 if ((rel.RelationshipState == "sister") || (rel.RelationshipState == "brother"))
                                 {
                                     newState = "grandmother";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("granddaughter"):
                                 if ((rel.RelationshipState == "wife") || (rel.RelationshipState == "husband"))
                                 {
                                     newState = "granddaughter";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("grandson"):
                                 if ((rel.RelationshipState == "wife") || (rel.RelationshipState == "husband"))
                                 {
                                     newState = "grandson";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("nephew"):
                                 if ((rel.RelationshipState == "father") || (rel.RelationshipState == "mother"))
                                 {
                                     newState = "grandson";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             case ("niece"):
                                 if ((rel.RelationshipState == "father") || (rel.RelationshipState == "mother"))
                                 {
                                     newState = "granddaughter";
                                     relationship = new Relationship { RelationshipID = 0, FirstPersonID = relativeID, SecondPersonID = rel.Person.PersonID, State = newState };
                                 }
                                 break;
                             #endregion
                         }
                         if (relationship != null) Relationships.Insert(relationship);
                     }
                 }
             }
             Context.SubmitChanges();
         }
         catch (Exception e)
         {
             message = e.Message;
         }
         return message;
     }
     finally
     {
         if (ToDispose) Context.Dispose();
     }
 }