public IHttpActionResult PostAssociateWithSkills(AssociateWithSkillsDTO associateWithSkillsDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var result = _associateBAL.AddAssociateWithSkills(associateWithSkillsDTO);

            return(Ok(result));
        }
        public void AddNewAssociateWithSkills_ValidAssociate_ReturnsAllAssociates()
        {
            bool isValidAssociate = false;
            List <AssociateSkillsDTO> associateSkills = null;

            while (!isValidAssociate)
            {
                var associateId = new Random().Next();

                var associateInDb   = _associateController.Get(associateId);
                var associateResult = associateInDb as OkNegotiatedContentResult <IEnumerable <AssociateDTO> >;
                if (associateResult == null)
                {
                    isValidAssociate = true;

                    IHttpActionResult skillsActionResult = new AssociateSkillsController().Get();
                    var skillsContentResult = skillsActionResult as OkNegotiatedContentResult <IEnumerable <AssociateSkillsDTO> >;

                    if (skillsContentResult != null)
                    {
                        associateSkills = new List <AssociateSkillsDTO>();
                        associateSkills.Add(skillsContentResult.Content.FirstOrDefault());
                    }

                    var associate = new AssociateWithSkillsDTO()
                    {
                        Associate = new AssociateDTO()
                        {
                            AssociateId = associateId,
                            Name        = String.Format("Test Associate {0}", new Random().Next()),
                            Email       = String.Format("associate{0}@skillstracker.com", new Random().Next()),
                            Mobile      = "9834509344",
                            StatusGreen = true,
                            StatusBlue  = false,
                            StatusRed   = false,
                            Level1      = true,
                            Level2      = false,
                            Level3      = false,
                            Strength    = "Strength",
                            Weakness    = "Weakness",
                            Remark      = "Remark"
                        },
                        Skills = associateSkills != null?associateSkills.ToArray() : null
                    };

                    IHttpActionResult actionResult = _associateController.PostAssociateWithSkills(associate);
                    var contentResult = actionResult as OkNegotiatedContentResult <bool>;

                    Assert.IsNotNull(contentResult);
                    Assert.IsTrue(contentResult.Content);
                }
            }
        }
        public IHttpActionResult Put()
        {
            AssociateWithSkillsDTO associateDTO = null;
            string imageUrl    = string.Empty;
            var    httpRequest = HttpContext.Current.Request;

            foreach (var key in httpRequest.Form.AllKeys)
            {
                var data = httpRequest.Form[key];
                associateDTO = JsonConvert.DeserializeObject <AssociateWithSkillsDTO>(data);
            }

            if (associateDTO != null)
            {
                var associateInDb = _associateBAL.GetAssociate(associateDTO.Associate.AssociateId);

                if (associateInDb != null)
                {
                    if (httpRequest.Files.Count > 0)
                    {
                        var postedFile = httpRequest.Files[0];
                        var imagePath  = HttpContext.Current.Server.MapPath("~/Images/");
                        if (!Directory.Exists(imagePath))
                        {
                            Directory.CreateDirectory(imagePath);
                        }

                        var fileName = String.Format("{0}_{1}{2}", associateDTO.Associate.AssociateId, DateTime.Now.ToString("MMddyyyHHmmss"), Path.GetExtension(postedFile.FileName));

                        var filePath = Path.Combine(imagePath, fileName);

                        postedFile.SaveAs(filePath);

                        imageUrl = String.Format("{0}/{1}/{2}", ConfigurationManager.AppSettings["ApiBaseURL"], "Images", fileName);
                        associateDTO.Associate.Picture = imageUrl;
                    }

                    var response = _associateBAL.UpdateAssociateWithSkills(associateDTO);

                    return(Ok(new { status = response, ImageUrl = imageUrl }));
                }
                else
                {
                    var response = string.Format("Employee with ID: \"{0}\" does not exits. ", associateInDb.AssociateId);
                    return(BadRequest(response));
                }
            }

            return(BadRequest("Error in data"));
        }
示例#4
0
        public bool AddAssociateWithSkills(AssociateWithSkillsDTO associateDTO)
        {
            bool saved = false;

            var associate = Mapper.Map <Associate>(associateDTO.Associate);
            var skills    = associateDTO.Skills.Select(Mapper.Map <AssociateSkillsDTO, AssociateSkills>);

            using (var unitOfWork = new UnitOfWork(new SkillsTrackerContext()))
            {
                unitOfWork.Associates.Add(associate);
                saved = unitOfWork.Complete() == 1;

                if (skills != null && skills.Count() > 0)
                {
                    unitOfWork.AssociateSkills.AddRange(skills);
                    var result = unitOfWork.Complete();
                    saved = result > 0;
                }

                return(saved);
            }
        }
示例#5
0
        public bool UpdateAssociateWithSkills(AssociateWithSkillsDTO associateDTO)
        {
            bool saved = false;

            using (var unitOfWork = new UnitOfWork(new SkillsTrackerContext()))
            {
                try
                {
                    var associateInDB = unitOfWork.Associates.Get(associateDTO.Associate.AssociateId);
                    var skillsInDb    = unitOfWork.AssociateSkills.GetAssociateSkillByAssociateId(associateDTO.Associate.AssociateId);

                    if (associateInDB != null)
                    {
                        Mapper.Map(associateDTO.Associate, associateInDB);
                        saved = unitOfWork.Complete() > 0;
                    }

                    if (skillsInDb != null)
                    {
                        if (associateDTO.Skills != null)
                        {
                            var skillsExcluded = skillsInDb.Where(skill => !associateDTO.Skills.Any(skillFromUpdate => skillFromUpdate.SkillId == skill.SkillId));

                            if (skillsExcluded != null && skillsExcluded.Count() > 0)
                            {
                                unitOfWork.AssociateSkills.RemoveRange(skillsExcluded);
                                saved = unitOfWork.Complete() > 0;
                            }
                        }
                    }

                    if (associateDTO.Skills != null)
                    {
                        var skillsIncluded = associateDTO.Skills.Where(skill => !skillsInDb.Any(skillFromDb => skillFromDb.SkillId == skill.SkillId));

                        if (skillsIncluded != null && skillsIncluded.Count() > 0)
                        {
                            var skills = Mapper.Map <IEnumerable <AssociateSkills> >(skillsIncluded);
                            unitOfWork.AssociateSkills.AddRange(skills);
                            saved = unitOfWork.Complete() > 0;
                        }

                        if (skillsInDb != null)
                        {
                            if (skillsInDb.Count() > 0)
                            {
                                var skills = associateDTO.Skills.Where(skill => skillsInDb.Any(skillFromDb => skillFromDb.SkillId == skill.SkillId));

                                foreach (var skill in skills)
                                {
                                    skillsInDb.Where(newSkill => newSkill.SkillId == skill.SkillId).First().SkillRating = skill.SkillRating;
                                }

                                saved = unitOfWork.Complete() > 0;
                            }
                        }
                    }

                    saved = true;
                }
                catch
                {
                    saved = false;
                }
            }

            return(saved);
        }