public async Task <RelationDetailsViewModel> PostRelationAsync(RelationDetailsCreateModel relationModel)
        {
            TryFormatPostalCode(relationModel);

            Relation relation = new Relation()
            {
                Id              = relationModel.Id,
                Name            = relationModel.Name,
                FullName        = relationModel.FullName,
                TelephoneNumber = relationModel.TelephoneNumber,
                EmailAddress    = relationModel.EmailAddress
            };

            RelationAddress relationAddress = new RelationAddress()
            {
                RelationId  = relation.Id,
                CountryName = relationModel.Country,
                City        = relationModel.City,
                Street      = relationModel.Street,
                Number      = relationModel.StreetNumber,
                PostalCode  = relationModel.PostalCode
            };

            _context.Relations.Add(relation);
            _context.RelationAddresses.Add(relationAddress);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                if (RelationExists(relation.Id))
                {
                    throw ex;
                }
                else
                {
                    throw;
                }
            }

            RelationDetailsViewModel relationView = await _context.Relations.Where(r => r.Id == relation.Id).Select(v => new RelationDetailsViewModel
            {
                Id              = v.Id,
                Name            = v.Name,
                FullName        = v.FullName,
                TelephoneNumber = v.TelephoneNumber,
                EmailAddress    = v.EmailAddress,
                Country         = v.RelationAddress.CountryName,
                City            = v.RelationAddress.City,
                Street          = v.RelationAddress.Street,
                StreetNumber    = v.RelationAddress.Number,
                PostalCode      = v.RelationAddress.PostalCode
            }).FirstOrDefaultAsync();

            return(relationView);
        }
Exemple #2
0
        //Create Common Model With Common Lists
        RelationDetailsViewModel RelDetailsModel(RelationDetailsViewModel model = null)
        {
            if (model == null)
            {
                model = new RelationDetailsViewModel();
                //model.RelTypeId = 0;
                //model.EmpId = 0;
            }

            model.ListEmployee        = _unitOfWork.EmployeeRepository.GetAll().ToList();
            model.ListRelationType    = _unitOfWork.RelationTypeRepository.GetAll().ToList();
            model.ListRelationDetails = _unitOfWork.RelationDetailsRepository.GetAll().OrderBy(x => x.Employee.EmpName).ToList();

            //List<RelationTypeList> relType = new List<RelationTypeList>();

            //RelationTypeList rel = new RelationTypeList()
            //{
            //    Id = -1,
            //    Name = "Add New Type",
            //};

            //relType.Add(rel);

            //RelationTypeList rel1 = new RelationTypeList()
            //{
            //    Id = 0,
            //    Name = "--Select Relation Type--",
            //};

            //relType.Add(rel1);

            //var relTypeList = _unitOfWork.RelationTypeRepository.GetAll().ToList();

            //foreach (var item in relTypeList)
            //{
            //    RelationTypeList type = new RelationTypeList()
            //    {
            //        Id = item.Id,
            //        Name = item.RelationType1,
            //    };

            //    relType.Add(type);
            //}

            //model.ListRelationType = relType;

            //model.RelTypeId = -1;
            //model.EmpId = 0;

            return(model);
        }
Exemple #3
0
        public ActionResult AddRelationDetails(RelationDetailsViewModel model)
        {
            model = RelDetailsModel(model);

            try
            {
                if (ModelState.IsValid)
                {
                    var maxCount = _unitOfWork.RelationTypeCountRepository.GetAll(x => x.EmpId == model.EmpId && x.RelTypeId == model.RelTypeId).FirstOrDefault();

                    int relCount = 0;


                    //If there hasn't any record according to this empId and relId, Insert Record to DB_RelationTypeCount and get maximum count for relevant empId and relId
                    if (maxCount == null)
                    {
                        RelationTypeCount count = new RelationTypeCount()
                        {
                            EmpId     = model.EmpId,
                            RelTypeId = model.RelTypeId,
                            MaxCount  = 1
                        };

                        _unitOfWork.RelationTypeCountRepository.Insert(count);

                        if (_unitOfWork.Save() <= 0)
                        {
                            TempData[MessaageEnum.message.ToString()] = Messages._failed;
                            //return RedirectToAction("RelDetailsIndex");
                            return(View("RelDetailsIndex", model));
                        }

                        relCount = 1;
                    }
                    else
                    {
                        relCount = (int)maxCount.MaxCount;
                    }

                    var existObj = _unitOfWork.RelationDetailsRepository.GetAll(x => x.EmpId == model.EmpId && x.RelationTypeId == model.RelTypeId).ToList();

                    //If maximum Count is less than counts of db records of relevant empId and relId, Then Insert
                    if (existObj.Count < relCount)
                    {
                        bool hasName = false;

                        //Check whether relation name is already exist or not
                        foreach (var item in existObj)
                        {
                            if (item.RelationName.ToUpper() == model.RelName.ToUpper())
                            {
                                hasName = true;
                            }
                        }

                        if (!hasName)
                        {
                            RelationDetail detail = new RelationDetail()
                            {
                                EmpId          = model.EmpId,
                                RelationTypeId = model.RelTypeId,
                                RelationName   = model.RelName
                            };

                            _unitOfWork.RelationDetailsRepository.Insert(detail);
                        }
                        else
                        {
                            TempData[MessaageEnum.message.ToString()] = "danger_This relation name is already exist";
                            return(View("RelDetailsIndex", model));
                        }
                    }
                    else
                    {
                        TempData[MessaageEnum.message.ToString()] = "danger_You can add maximum " + relCount + ", from this relation type";
                        return(View("RelDetailsIndex", model));
                        //ModelState.AddModelError("RelTypeId", "You can add maximum " + relCount + ", from this relation type");
                    }

                    if (_unitOfWork.Save() > 0)
                    {
                        TempData[MessaageEnum.message.ToString()] = Messages._sucess;
                        return(RedirectToAction("RelDetailsIndex"));
                    }
                    else
                    {
                        TempData[MessaageEnum.message.ToString()] = Messages._failed;
                        return(View("RelDetailsIndex", model));
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return(View("RelDetailsIndex", model));
        }