Esempio n. 1
0
        /// <summary>
        /// Insert Request
        /// </summary>
        /// <param name="requestViewModel"></param>
        /// <returns></returns>
        public IResult InsertRequest(List <FileDetails> fileList, RequestViewModel requestViewModel)
        {
            requestViewModel.RequestId = null;
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                var request = new Request();
                request.MailUsers = new List <MailUserDetails>();
                if (requestViewModel.MailUsers.Any())
                {
                    requestViewModel.MailUsers.ForEach(user =>
                    {
                        var mailUser = new MailUserDetails();
                        mailUser.MapFromViewModel(user);
                        request.MailUsers.Add(mailUser);
                    });
                }
                request.MapFromViewModel(requestViewModel);
                request.CreatedDate  = GenericHelper.CurrentDate;
                request.ModifiedDate = GenericHelper.CurrentDate;
                request.IsActive     = true;
                _requestRepository.InsertOne(request);

                if (fileList != null && fileList.Any())
                {
                    request.Files = new List <ObjectId>();
                    fileList.ForEach(fileViewModel =>
                    {
                        var file = new File();
                        file.MapFromViewModel(fileViewModel);
                        file.CreatedDate  = GenericHelper.CurrentDate;
                        file.ModifiedDate = GenericHelper.CurrentDate;
                        file.IsActive     = true;
                        _fileRepository.InsertOne(file);
                        request.Files.Add(file.FileId);
                    });
                }
                var updateDefinition = Builders <Request> .Update.Set(t => t.Files, request.Files);

                _requestRepository.UpdateOne(t => t.RequestId.Equals(request.RequestId), updateDefinition);
                if (request.Files != null && request.Files.Any())
                {
                    requestViewModel.Files = new List <string>();
                    request.Files.ForEach(x => requestViewModel.Files.Add(x.ToString()));
                }
                result.Body    = requestViewModel.MapFromModel(request);
                result.Message = RequestNotification.Created;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status  = Status.Fail;
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Update Request
        /// </summary>
        /// <param name="requestViewModel"></param>
        /// <returns></returns>
        public IResult UpdateRequest(List <FileDetails> fileList, RequestViewModel requestViewModel)
        {
            var result = new Result
            {
                Operation = Operation.Update,
                Status    = Status.Success
            };

            try
            {
                var listFiles = new List <ObjectId>();
                if (fileList != null && fileList.Any())
                {
                    fileList.ForEach(fileViewModel =>
                    {
                        var file = new File();
                        file.MapFromViewModel(fileViewModel);
                        file.CreatedDate  = GenericHelper.CurrentDate;
                        file.ModifiedDate = GenericHelper.CurrentDate;
                        file.IsActive     = true;
                        _fileRepository.InsertOne(file);
                        listFiles.Add(file.FileId);
                    });
                }

                var mailUsers = new List <MailUserDetails>();
                if (requestViewModel.MailUsers.Any())
                {
                    requestViewModel.MailUsers.ForEach(user =>
                    {
                        var mailUser = new MailUserDetails();
                        mailUser.MapFromViewModel(user);
                        mailUsers.Add(mailUser);
                    });
                }

                var updateDefinition = Builders <Request> .Update
                                       .Set(x => x.Type, requestViewModel.Type)
                                       .Set(x => x.Description, requestViewModel.Description)
                                       .Set(x => x.FromDate, requestViewModel.FromDate)
                                       .Set(x => x.ToDate, requestViewModel.ToDate)
                                       .Set(x => x.RequestedBy, requestViewModel.RequestedBy)
                                       .Set(x => x.Company, requestViewModel.Company)
                                       .Set(x => x.PhoneNumber, requestViewModel.PhoneNumber)
                                       .AddToSetEach(x => x.MailUsers, mailUsers)
                                       .AddToSetEach(x => x.Files, listFiles);


                _requestRepository.UpdateOne(t => t.RequestId.Equals(ObjectId.Parse(requestViewModel.RequestId)), updateDefinition);
                result.Message = RequestNotification.Updated;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status  = Status.Fail;
            }
            return(result);
        }
        /// <summary>
        /// Insert Material Request
        /// </summary>
        /// <param name="materialRequestViewModel"></param>
        /// <returns></returns>
        public IResult InsertMaterialRequest(List <FileDetails> fileList, MaterialRequestViewModel materialRequestViewModel)
        {
            materialRequestViewModel.RequestId = null;
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                var materialRequest = new Request();
                materialRequest.MapFromViewModel(materialRequestViewModel, (ClaimsIdentity)_principal.Identity);

                materialRequest.MailUsers = new List <MailUserDetails>();
                if (materialRequestViewModel.MailUsers != null && materialRequestViewModel.MailUsers.Any())
                {
                    materialRequest.MailUsers = materialRequestViewModel.MailUsers.Select(user =>
                    {
                        var mailUser = new MailUserDetails();
                        mailUser.MapFromViewModel(user);
                        return(mailUser);
                    }).ToList();
                }

                if (!string.IsNullOrEmpty(materialRequestViewModel.RegardingId))
                {
                    materialRequest.RegardingId = ObjectId.Parse(materialRequestViewModel.RegardingId);
                }
                _requestRepository.InsertOne(materialRequest);

                if (fileList != null && fileList.Any())
                {
                    materialRequest.Files = fileList.Select(fileViewModel =>
                    {
                        var file = new File();
                        file.MapFromViewModel(fileViewModel, (ClaimsIdentity)_principal.Identity);
                        _fileRepository.InsertOne(file);
                        return(file.FileId);
                    }).ToList();
                }

                var updateDefinition = Builders <Request> .Update.Set(t => t.Files, materialRequest.Files);

                _requestRepository.UpdateOne(t => t.RequestId.Equals(materialRequest.RequestId), updateDefinition);

                if (materialRequest.Files != null && materialRequest.Files.Any())
                {
                    materialRequestViewModel.Files = new List <string>();
                    materialRequest.Files.ForEach(x => materialRequestViewModel.Files.Add(x.ToString()));
                }
                result.Body    = materialRequestViewModel.MapFromModel(materialRequest);
                result.Message = RequestNotification.Created;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status  = Status.Fail;
            }
            return(result);
        }