Example #1
0
        public AspUserService(ApplicationDbContext db, Controller controller)
        {
            User       = db._BCPUsers.Where(e => e.AspUser.Id.Equals(controller.User.FindFirstValue(ClaimTypes.NameIdentifier)) && e.Deleted == false).FirstOrDefault();
            IsValid    = false;
            IsAdmin    = false;
            IsLecturer = false;

            if (User != null)
            {
                if (User.Status > 0)
                {
                    IsValid = true;
                }

                if (User.Status == 2)
                {
                    IsLecturer = true;
                }

                if (User.Status == 3)
                {
                    IsAdmin = true;
                }

                if (User.Status == 4)
                {
                    IsLecturer = true;
                    IsAdmin    = true;
                }
            }
        }
Example #2
0
        public StudentInfoOutput AddToClass([FromBody] StudentInfoInput input)
        {
            StudentInfoOutput output = new StudentInfoOutput();

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                Class thisClass = _db.Classes.Where(e => e.ClassCode.ToUpper().Equals(input.ClassCode.ToUpper()) && e.Deleted == false).FirstOrDefault();
                if (thisClass == null)
                {
                    Response.StatusCode = 400;
                    output.Result       = "CLASS_NOT_EXIST";
                }
                else
                {
                    AspUserService aspUser = new AspUserService(_db, this);
                    if (aspUser.IsAdmin)
                    {
                        BCPUser student = _db._BCPUsers.Where(e => e.Id.Equals(input.StudentId) && e.Deleted == false).FirstOrDefault();
                        if (student == null)
                        {
                            Response.StatusCode = 400;
                            output.Result       = "STUDENT_NOT_EXIST";
                        }
                        else
                        {
                            ClassAllocation ca = _db.ClassAllocations.Where(e => e.Class == thisClass && e.Student == student && e.Deleted == false).FirstOrDefault();
                            if (ca == null)
                            {
                                ClassAllocation newCa = new ClassAllocation()
                                {
                                    Class   = thisClass,
                                    Student = student
                                };

                                _db.ClassAllocations.Add(newCa);
                                _db.SaveChanges();
                                output.Result = "OK";
                            }
                            else
                            {
                                Response.StatusCode = 400;
                                output.Result       = "ALREAD_ADDED";
                            }
                        }
                    }
                    else
                    {
                        Response.StatusCode = 400;
                        output.Result       = "NO_PRIVILEGE";
                    }
                }
            }

            return(output);
        }
Example #3
0
        public UserInfoOutput CheckUserById([FromBody] UserInfoInput input)
        {
            UserInfoOutput output  = new UserInfoOutput();
            AspUserService aspUser = new AspUserService(_db, this);

            if (aspUser.IsAdmin)
            {
                if (input == null)
                {
                    Response.StatusCode = 400;
                    output.Result       = "INPUT_IS_NULL";
                }
                else
                {
                    BCPUser user = _db._BCPUsers.Where(e => e.Id.Equals(input.UserId)).FirstOrDefault();
                    if (user == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "USER_NOT_EXIST";
                    }
                    else
                    {
                        output.Email  = user.Email;
                        output.Name   = user.Name;
                        output.Result = "OK";
                    }
                }
            }
            else
            {
                Response.StatusCode = 400;
                output.Result       = "NO_PRIVILEGE";
            }
            return(output);
        }
        public IActionResult GetDetail(string id)
        {
            if (User.IsInRole("ADMIN"))
            {
                StudentViewModel model = new StudentViewModel();

                BCPUser user = _db._BCPUsers.Where(e => e.Id.Equals(id)).FirstOrDefault();
                if (user == null)
                {
                    return(RedirectToAction("Index", "Student"));
                }
                else
                {
                    model.StudentName   = user.Name;
                    model.AccountRole   = user.Status;
                    model.StudentImages = user.List_UserImage.Where(e => e.Deleted == false && e.Status == 2).OrderByDescending(e => e.Confidence).ToList();
                    model.StudentId     = id;
                    model.StudentEmail  = user.Email;
                }

                ViewBag.SiteUrl = _db.SiteConfigs.Where(e => e.Key.Equals("SITEURL")).First().Value;
                return(View(model));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public RecognizerTaskOutput StudentImageCapture([FromBody] RecognizerTaskInput input)
        {
            RecognizerTaskOutput output = new RecognizerTaskOutput();

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                AspUserService aspUser = new AspUserService(_db, this);
                if (aspUser.IsAdmin)
                {
                    Recognizer recognizer = _db.Recognizers.Where(e => e.Id.Equals(input.RecognizerId) && e.Deleted == false).FirstOrDefault();
                    BCPUser    student    = _db._BCPUsers.Where(e => e.Id.Equals(input.StudentId) && e.Deleted == false).FirstOrDefault();

                    if (recognizer == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "RECOGNIZER_NOT_FOUND";
                    }
                    else if (student == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "STUDENT_NOT_FOUND";
                    }
                    else
                    {
                        List <UserImage> studentImages = student.List_UserImage.Where(e => e.Deleted == false && e.Status != 0).ToList();
                        foreach (UserImage item in studentImages)
                        {
                            item.Status = 0;
                        }

                        RecognizerTask task = new RecognizerTask()
                        {
                            Command        = "REGISTER_NEW_FACE",
                            Status         = 1,
                            Recognizer     = recognizer,
                            PrimaryValue   = student.Id,
                            SecondaryValue = (int.Parse(_db.SiteConfigs.Where(e => e.Key.Equals("NUM_PHOTO_PER_STUDENT")).FirstOrDefault().Value) + 5).ToString()
                        };

                        _db.RecognizerTasks.Add(task);
                        _db.SaveChanges();

                        output.RecognizerTaskId = task.Id;
                        output.Result           = "OK";
                    }
                }
                else
                {
                    Response.StatusCode = 400;
                    output.Result       = "NO_PRIVILEGE";
                }
            }

            return(output);
        }
        public IActionResult AddFace(string id)
        {
            AspUserService aspUser = new AspUserService(_db, this);

            if (aspUser.IsAdmin)
            {
                BCPUser student = _db._BCPUsers.Where(e => e.Id.Equals(id)).FirstOrDefault();

                if (student == null)
                {
                    return(RedirectToAction("Index", "Student"));
                }
                else
                {
                    List <Recognizer> recognizers  = _db.Recognizers.Where(e => e.Deleted == false).OrderBy(e => e.Id).ToList();
                    StudentViewModel  studentModel = new StudentViewModel();
                    AddFaceViewModel  model        = new AddFaceViewModel();

                    studentModel.StudentName   = student.Name;
                    studentModel.AccountRole   = student.Status;
                    studentModel.StudentImages = student.List_UserImage.Where(e => e.Deleted == false && e.Status == 2).OrderByDescending(e => e.Confidence).ToList();
                    studentModel.StudentId     = id;

                    ViewBag.SiteUrl   = _db.SiteConfigs.Where(e => e.Key.Equals("SITEURL")).First().Value;
                    model.Student     = studentModel;
                    model.Recognizers = recognizers;

                    return(View(model));
                }
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public ClassInfoOutput Change([FromBody] ClassInfoInput input)
        {
            ClassInfoOutput output = new ClassInfoOutput();

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                AspUserService aspUser = new AspUserService(_db, this);
                if (aspUser.IsAdmin)
                {
                    Class thisClass = _db.Classes.Where(e => e.Id.Equals(input.ClassId) && e.Deleted == false).FirstOrDefault();
                    if (thisClass == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "CLASS_NOT_EXIST";
                    }
                    else
                    {
                        BCPUser lecturer = _db._BCPUsers.Where(e => e.Id.Equals(input.LecturerId) && e.Deleted == false).Where(e => e.Status == 2 || e.Status == 4).FirstOrDefault();
                        if (lecturer == null && !string.IsNullOrEmpty(input.LecturerId))
                        {
                            Response.StatusCode = 400;
                            output.Result       = "LECTURER_NOT_EXIST";
                        }
                        else
                        {
                            thisClass.Capacity = input.Capacity;
                            thisClass.Name     = input.ClassName;
                            if (string.IsNullOrEmpty(input.LecturerId))
                            {
                                thisClass.Lecturer = null;
                            }
                            else
                            {
                                thisClass.Lecturer = lecturer;
                            }

                            _db.SaveChanges();
                            output.Result = "OK";
                        }
                    }
                }
                else
                {
                    Response.StatusCode = 400;
                    output.Result       = "NO_PRIVILEGE";
                }
            }

            return(output);
        }
Example #8
0
        public StudentPendingPhotoOutput RetrievePendingPhoto([FromBody] StudentPendingPhotoInput input)
        {
            StudentPendingPhotoOutput output = new StudentPendingPhotoOutput();

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                AspUserService aspUser = new AspUserService(_db, this);
                if (aspUser.IsAdmin)
                {
                    BCPUser student = _db._BCPUsers.Where(e => e.Id.Equals(input.StudentId)).FirstOrDefault();

                    if (student == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "STUDENT_NOT_EXIST";
                    }
                    else
                    {
                        List <UserImage>        images     = student.List_UserImage.Where(e => e.Deleted == false && e.Status == 1).OrderByDescending(e => e.Confidence).ToList();
                        List <PendingPhotoItem> photoItems = new List <PendingPhotoItem>();
                        string siteUrl = _db.SiteConfigs.Where(e => e.Key.Equals("SITEURL")).First().Value;

                        foreach (UserImage item in images)
                        {
                            PendingPhotoItem newPhotoItem = new PendingPhotoItem()
                            {
                                UserImageId = item.Id,
                                Url         = siteUrl + "/" + item.Url,
                                DateAdded   = item.DateCreated
                            };

                            photoItems.Add(newPhotoItem);
                        }

                        output.Photos = photoItems;
                        output.Result = "OK";
                    }
                }
                else
                {
                    Response.StatusCode = 400;
                    output.Result       = "NO_PRIVILEGE";
                }
            }

            return(output);
        }
Example #9
0
        public UserInfoOutput AddByEmail([FromBody] UserInfoInput input)
        {
            AspUserService aspUser = new AspUserService(_db, this);
            UserInfoOutput output  = new UserInfoOutput();

            if (aspUser.IsAdmin)
            {
                BCPUser user = _db._BCPUsers.Where(e => e.Email.ToUpper().Equals(input.Email.ToUpper()) && e.Deleted == false).FirstOrDefault();
                if (user == null)
                {
                    Response.StatusCode = 400;
                    output.Result       = "USER_NOT_EXIST";
                }
                else
                {
                    if (user.Status == 2 || user.Status == 4)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "USER_ALREADY_ASSIGNED_LECTURER";
                    }
                    else
                    {
                        if (user.Status == 3)
                        {
                            user.Status = 4;
                        }
                        else
                        {
                            user.Status = 2;
                        }
                        _db.SaveChanges();

                        output.Result = "OK";
                    }
                }
            }
            else
            {
                Response.StatusCode = 400;
                output.Result       = "NO_PRIVILEGE";
            }


            return(output);
        }
        public IActionResult SetClass(string id)
        {
            BCPUser student = _db._BCPUsers.Where(e => e.Id.Equals(id)).FirstOrDefault();

            if (student == null)
            {
                return(RedirectToAction("Index", "Student"));
            }
            else
            {
                AspUserService aspUser = new AspUserService(_db, this);
                if (aspUser.IsAdmin)
                {
                    SetClassViewModel   model        = new SetClassViewModel();
                    StudentViewModel    studentModel = new StudentViewModel();
                    List <SetClassItem> classModel   = new List <SetClassItem>();
                    studentModel.StudentName   = student.Name;
                    studentModel.AccountRole   = student.Status;
                    studentModel.StudentImages = student.List_UserImage.Where(e => e.Deleted == false && e.Status == 2).OrderByDescending(e => e.Confidence).ToList();
                    studentModel.StudentId     = id;
                    List <ClassAllocation> classes = student.List_ClassAllocation.Where(e => e.Deleted == false).ToList();

                    foreach (ClassAllocation item in classes)
                    {
                        SetClassItem newClassItem = new SetClassItem()
                        {
                            ClassName  = item.Class.Name,
                            DateJoined = item.DateCreated
                        };

                        classModel.Add(newClassItem);
                    }

                    model.Classes = classModel;
                    model.Student = studentModel;
                    return(View(model));
                }
                else
                {
                    return(RedirectToAction("Index", "Student"));
                }
            }
        }
Example #11
0
        public UserInfoOutput Remove([FromBody] UserInfoInput input)
        {
            UserInfoOutput output  = new UserInfoOutput();
            AspUserService aspUser = new AspUserService(_db, this);

            if (aspUser.IsAdmin)
            {
                if (input == null)
                {
                    Response.StatusCode = 400;
                    output.Result       = "INPUT_IS_NULL";
                }
                else
                {
                    BCPUser user = _db._BCPUsers.Where(e => e.Id.Equals(input.UserId)).FirstOrDefault();
                    if (user == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "USER_NOT_EXIST";
                    }
                    else
                    {
                        if (user.Status == 4)
                        {
                            user.Status = 3;
                        }
                        else
                        {
                            user.Status = 1;
                        }

                        _db.SaveChanges();
                        output.Result = "OK";
                    }
                }
            }
            else
            {
                Response.StatusCode = 400;
                output.Result       = "NO_PRIVILEGE";
            }
            return(output);
        }
Example #12
0
        public StudentInfoOutput RemoveFace([FromBody] StudentInfoInput input)
        {
            StudentInfoOutput output  = new StudentInfoOutput();
            AspUserService    aspUser = new AspUserService(_db, this);

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                if (aspUser.IsAdmin)
                {
                    BCPUser user = _db._BCPUsers.Where(e => e.Id.Equals(input.StudentId)).FirstOrDefault();
                    if (user == null)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "USER_NOT_EXIST";
                    }
                    else
                    {
                        List <UserImage> images = user.List_UserImage.Where(e => e.Deleted == false && e.Status == 2).ToList();
                        foreach (UserImage item in images)
                        {
                            item.Status = 0;
                        }

                        _db.SaveChanges();
                        output.Result = "OK";
                    }
                }
                else
                {
                    Response.StatusCode = 400;
                    output.Result       = "NO_PRIVILEGE";
                }
            }

            return(output);
        }
Example #13
0
        public async Task <StudentInfoOutput> ProcessPhoto([FromBody] StudentInfoInput input)
        {
            StudentInfoOutput output  = new StudentInfoOutput();
            AspUserService    aspUser = new AspUserService(_db, this);
            bool failure = false;

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                string            personGroupId = _db.SiteConfigs.Where(e => e.Key.Equals("PERSONGROUP")).FirstOrDefault().Value;
                Uri               uri           = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/persongroups/" + personGroupId + "/training");
                HttpClientHandler handler       = new HttpClientHandler();
                StringContent     queryString   = null;
                HttpClient        client        = new HttpClient(handler);
                //string respond = null;
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                HttpResponseMessage response = await client.GetAsync(uri);

                string siteUrl = _db.SiteConfigs.Where(e => e.Key.Equals("SITEURL")).First().Value;
                string respond = await response.Content.ReadAsStringAsync();

                dynamic jsonObj        = JsonConvert.DeserializeObject(respond);
                string  trainingStatus = "NONE";
                if (jsonObj.status != null)
                {
                    trainingStatus = jsonObj.status;
                }
                else
                {
                    trainingStatus = "NONE";
                }

                if (trainingStatus.Equals("running"))
                {
                    Response.StatusCode = 400;
                    output.Result       = "TRAINING_IN_PROGRESS";
                }
                else
                {
                    if (aspUser.IsAdmin)
                    {
                        BCPUser user = _db._BCPUsers.Where(e => e.Id.Equals(input.StudentId)).FirstOrDefault();

                        if (user == null)
                        {
                            Response.StatusCode = 400;
                            output.Result       = "USER_NOT_FOUND";
                        }
                        else
                        {
                            List <UserImage> imageStore    = user.List_UserImage.Where(e => e.Status == 1).OrderBy(e => e.DateCreated).ToList();
                            List <string>    imageToBeUsed = new List <string>();
                            int    min             = int.Parse(_db.SiteConfigs.Where(e => e.Key.Equals("NUM_PHOTO_PER_STUDENT")).FirstOrDefault().Value);
                            string mainSiteUrl     = _db.SiteConfigs.Where(e => e.Key.Equals("SITEURL")).FirstOrDefault().Value;
                            int    imageCount      = imageStore.Count;
                            string faceIdToCompare = null;

                            if (imageCount >= min)
                            {
                                string faceListId = Guid.NewGuid().ToString().ToLower();
                                uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/facelists/" + faceListId);
                                handler     = new HttpClientHandler();
                                queryString = null;
                                client      = new HttpClient(handler);
                                //string respond = null;
                                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                PersonGroupInput pgInput = new PersonGroupInput()
                                {
                                    Name             = faceListId,
                                    RecognitionModel = "recognition_02"
                                };

                                queryString = new StringContent(JsonConvert.SerializeObject(pgInput), Encoding.UTF8, "application/json");
                                queryString.Headers.Remove("Content-Type");
                                queryString.Headers.Add("Content-Type", "application/json");
                                response = await client.PutAsync(uri, queryString);

                                //respond = await response.Content.ReadAsStringAsync();

                                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                {
                                    foreach (UserImage item in imageStore)
                                    {
                                        if (imageCount > 1)
                                        {
                                            uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/facelists/" + faceListId + "/persistedFaces");
                                            handler     = new HttpClientHandler();
                                            queryString = null;
                                            client      = new HttpClient(handler);
                                            //string respond = null;
                                            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                            pgInput = new PersonGroupInput()
                                            {
                                                Url = mainSiteUrl + "/" + item.Url
                                            };

                                            queryString = new StringContent(JsonConvert.SerializeObject(pgInput), Encoding.UTF8, "application/json");
                                            queryString.Headers.Remove("Content-Type");
                                            queryString.Headers.Add("Content-Type", "application/json");
                                            response = await client.PostAsync(uri, queryString);

                                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                            {
                                                respond = await response.Content.ReadAsStringAsync();

                                                jsonObj     = JsonConvert.DeserializeObject(respond);
                                                item.FaceId = jsonObj.persistedFaceId;
                                            }

                                            _db.SaveChanges();
                                            imageCount--;
                                        }
                                        else
                                        {
                                            uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/detect?recognitionModel=recognition_02");
                                            handler     = new HttpClientHandler();
                                            queryString = null;
                                            client      = new HttpClient(handler);
                                            //string respond = null;
                                            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                            pgInput = new PersonGroupInput()
                                            {
                                                Url = mainSiteUrl + "/" + item.Url
                                            };

                                            queryString = new StringContent(JsonConvert.SerializeObject(pgInput), Encoding.UTF8, "application/json");
                                            queryString.Headers.Remove("Content-Type");
                                            queryString.Headers.Add("Content-Type", "application/json");
                                            response = await client.PostAsync(uri, queryString);

                                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                            {
                                                respond = await response.Content.ReadAsStringAsync();

                                                try
                                                {
                                                    jsonObj         = JsonConvert.DeserializeObject(respond);
                                                    item.Status     = 0;
                                                    faceIdToCompare = jsonObj[0].faceId;
                                                } catch (ArgumentOutOfRangeException e)
                                                {
                                                    e.ToString();
                                                }
                                            }

                                            _db.SaveChanges();
                                        }
                                    }

                                    uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/findsimilars");
                                    handler     = new HttpClientHandler();
                                    queryString = null;
                                    client      = new HttpClient(handler);
                                    //string respond = null;
                                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                    pgInput = new PersonGroupInput()
                                    {
                                        FaceId     = faceIdToCompare,
                                        FaceListId = faceListId,
                                        maxNumOfCandidatesReturned = 10
                                    };

                                    queryString = new StringContent(JsonConvert.SerializeObject(pgInput), Encoding.UTF8, "application/json");
                                    queryString.Headers.Remove("Content-Type");
                                    queryString.Headers.Add("Content-Type", "application/json");
                                    response = await client.PostAsync(uri, queryString);

                                    respond = await response.Content.ReadAsStringAsync();

                                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                    {
                                        respond = await response.Content.ReadAsStringAsync();

                                        jsonObj = JsonConvert.DeserializeObject(respond);
                                        foreach (var item in jsonObj)
                                        {
                                            string    id = item.persistedFaceId;
                                            UserImage ui = _db.UserImages.Where(e => e.FaceId.Equals(id)).FirstOrDefault();
                                            ui.Confidence = item.confidence;
                                            imageToBeUsed.Add(id);
                                            _db.SaveChanges();
                                        }
                                    }
                                    else
                                    {
                                        failure = true;
                                    }

                                    uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/facelists/" + faceListId);
                                    handler     = new HttpClientHandler();
                                    queryString = null;
                                    client      = new HttpClient(handler);
                                    //string respond = null;
                                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");

                                    queryString = new StringContent("", Encoding.UTF8, "application/json");
                                    queryString.Headers.Remove("Content-Type");
                                    queryString.Headers.Add("Content-Type", "application/json");
                                    response = await client.DeleteAsync(uri);

                                    if (imageToBeUsed.Count < min)
                                    {
                                        Response.StatusCode = 400;
                                        output.Result       = "IMAGE_USERSTORE_LESS_ACCURATE";
                                    }
                                    else
                                    {
                                        if (user.PersonId != null)
                                        {
                                            uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/persongroups/" + personGroupId + "/persons/" + user.PersonId);
                                            handler     = new HttpClientHandler();
                                            queryString = null;
                                            client      = new HttpClient(handler);
                                            //string respond = null;
                                            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");

                                            queryString = new StringContent("", Encoding.UTF8, "application/json");
                                            queryString.Headers.Remove("Content-Type");
                                            queryString.Headers.Add("Content-Type", "application/json");
                                            response = await client.DeleteAsync(uri);
                                        }

                                        if (response.StatusCode != System.Net.HttpStatusCode.OK)
                                        {
                                            failure = true;
                                        }

                                        uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/persongroups/" + personGroupId + "/persons");
                                        handler     = new HttpClientHandler();
                                        queryString = null;
                                        client      = new HttpClient(handler);
                                        //string respond = null;
                                        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                        pgInput = new PersonGroupInput()
                                        {
                                            Name = user.Id
                                        };
                                        queryString = new StringContent(JsonConvert.SerializeObject(pgInput), Encoding.UTF8, "application/json");
                                        queryString.Headers.Remove("Content-Type");
                                        queryString.Headers.Add("Content-Type", "application/json");
                                        response = await client.PostAsync(uri, queryString);

                                        if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                        {
                                            respond = await response.Content.ReadAsStringAsync();

                                            jsonObj       = JsonConvert.DeserializeObject(respond);
                                            user.PersonId = jsonObj.personId;
                                            _db.SaveChanges();
                                        }
                                        else
                                        {
                                            failure = true;
                                        }

                                        foreach (UserImage item in imageStore)
                                        {
                                            item.Status = 0;
                                            _db.SaveChanges();
                                        }

                                        foreach (string s in imageToBeUsed)
                                        {
                                            if (min > 0)
                                            {
                                                UserImage item = imageStore.Where(e => e.FaceId.Equals(s)).FirstOrDefault();
                                                uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/persongroups/" + personGroupId + "/persons/" + user.PersonId + "/persistedFaces");
                                                handler     = new HttpClientHandler();
                                                queryString = null;
                                                client      = new HttpClient(handler);
                                                //string respond = null;
                                                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                                pgInput = new PersonGroupInput()
                                                {
                                                    Url = mainSiteUrl + "/" + item.Url
                                                };
                                                queryString = new StringContent(JsonConvert.SerializeObject(pgInput), Encoding.UTF8, "application/json");
                                                queryString.Headers.Remove("Content-Type");
                                                queryString.Headers.Add("Content-Type", "application/json");
                                                response = await client.PostAsync(uri, queryString);

                                                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                                {
                                                    respond = await response.Content.ReadAsStringAsync();

                                                    jsonObj     = JsonConvert.DeserializeObject(respond);
                                                    item.FaceId = jsonObj.persistedFaceId;
                                                    item.Status = 2;
                                                    _db.SaveChanges();
                                                }
                                                else
                                                {
                                                    failure = true;
                                                }
                                            }
                                            min--;
                                        }

                                        uri         = new Uri("https://bcp-facial.cognitiveservices.azure.com/face/v1.0/persongroups/" + personGroupId + "/train");
                                        handler     = new HttpClientHandler();
                                        queryString = null;
                                        client      = new HttpClient(handler);
                                        //string respond = null;
                                        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "8d4c42ecbb784d909275492115ea56f0");
                                        queryString = new StringContent("", Encoding.UTF8, "application/json");
                                        queryString.Headers.Remove("Content-Type");
                                        queryString.Headers.Add("Content-Type", "application/json");
                                        response = await client.PostAsync(uri, queryString);

                                        if (failure)
                                        {
                                            Response.StatusCode = 500;
                                            output.Result       = "INCOMPLETE_PROCESS";
                                        }
                                        else
                                        {
                                            output.Result = "OK";
                                        }
                                    }
                                }
                                else
                                {
                                    Response.StatusCode = 500;
                                    output.Result       = "INTERNAL_ERROR";
                                }
                            }
                            else
                            {
                                Response.StatusCode = 400;
                                output.Result       = "USER_IMAGESTORE_LESS_MIN";
                            }
                        }
                    }
                    else
                    {
                        Response.StatusCode = 400;
                        output.Result       = "NO_PRIVILEGE";
                    }
                }
            }

            return(output);
        }
Example #14
0
        public async Task <UserInfoOutput> Create([FromBody] UserInfoInput input)
        {
            UserInfoOutput output = new UserInfoOutput();

            if (input == null)
            {
                Response.StatusCode = 400;
                output.Result       = "INPUT_IS_NULL";
            }
            else
            {
                if (string.IsNullOrEmpty(input.Email) || string.IsNullOrEmpty(input.Password) || string.IsNullOrEmpty(input.ConfirmPassword))
                {
                    Response.StatusCode = 400;
                    output.Result       = "INPUT_IS_NULL";
                }
                else
                {
                    if (input.Password.Length < 6)
                    {
                        Response.StatusCode = 400;
                        output.Result       = "PASSWORD_LENGTH_LESS_6";
                    }
                    else
                    {
                        if (input.Password.Equals(input.ConfirmPassword))
                        {
                            BCPUser user = _db._BCPUsers.Where(e => e.Email.Equals(input.Email)).FirstOrDefault();

                            if (user == null)
                            {
                                IdentityUser newAspUser = new IdentityUser()
                                {
                                    UserName = input.Email,
                                    Email    = input.Email
                                };

                                var status = await _userManager.CreateAsync(newAspUser, input.Password);

                                if (status.Succeeded)
                                {
                                    user = new BCPUser()
                                    {
                                        AspUser = newAspUser,
                                        Name    = input.Name,
                                        Email   = input.Email
                                    };

                                    _db._BCPUsers.Add(user);
                                    _db.SaveChanges();

                                    output.Result = "OK";
                                }
                                else
                                {
                                    Response.StatusCode = 500;
                                    output.Result       = "INTERNAL_ERROR";
                                }
                            }
                            else
                            {
                                Response.StatusCode = 400;
                                output.Result       = "USER_ALREADY_EXIST";
                            }
                        }
                        else
                        {
                            Response.StatusCode = 400;
                            output.Result       = "PASSWORD_MISMATCH";
                        }
                    }
                }
            }
            return(output);
        }