Пример #1
0
        public async Task <CommonResponse> ModifyUser(ModifyUserRequest userDetailValues)
        {
            using (var uow = this.unitOfWorkFactory.GetUnitOfWork())
            {
                var userCount = await uow.Repository <User>().Query().Select().Where(a => a.Id != userDetailValues.userId && a.UserName == userDetailValues.userName).CountAsync();

                if (userCount > 0)
                {
                    return(CommonResponse.CreateError("Username is already exits", "ERR001", ""));
                }

                var userValues = await uow.Repository <User>().Query().Select().SingleOrDefaultAsync(t => t.Id == userDetailValues.userId);

                if (userValues == null)
                {
                    return(CommonResponse.CreateError("Error", "Err02", "User Not found"));
                }
                userValues.Country      = userDetailValues.country;
                userValues.EmailId      = userDetailValues.emailId;
                userValues.FullName     = userDetailValues.name;
                userValues.UserName     = userDetailValues.userName;
                userValues.UserPassword = userValues.UserPassword;
                userValues.UserRole     = userDetailValues.userRole;
                userValues.City         = userDetailValues.city;

                var response = uow.SaveChanges();
                if (response.Response.status == false)
                {
                    return(CommonResponse.CreateError("Error occured while updating user.", "ERR001", ""));
                }

                return(CommonResponse.CreateSuccess("User updated successfully.", "SUC002", ""));
            }
        }
Пример #2
0
        public async Task <CommonResponse> AddUser(AddUserRequest newUserValues)
        {
            using (var uow = this.unitOfWorkFactory.GetUnitOfWork())
            {
                var user = await uow.Repository <User>().Query().Select().FirstOrDefaultAsync(a => a.UserName == newUserValues.userName);

                if (user != null)
                {
                    return(CommonResponse.CreateError("Username is already exits.", "ERR001", ""));
                }

                var newUser = new User
                {
                    FullName     = newUserValues.name,
                    Country      = newUserValues.country,
                    EmailId      = newUserValues.emailId,
                    State        = newUserValues.state,
                    PhoneNumber  = newUserValues.phoneNumber,
                    UserPassword = newUserValues.password,
                    UserName     = newUserValues.userName,
                    UserRole     = newUserValues.userRole,
                    City         = newUserValues.city
                };

                uow.Repository <User>().Add(newUser);
                var response = uow.SaveChanges();
                if (response.Response.status == false)
                {
                    return(CommonResponse.CreateError("Error occured while adding user.", "ERR001", ""));
                }

                return(CommonResponse.CreateSuccess("User added successfully.", "SUCC001", ""));
            }
        }
Пример #3
0
        public async Task <CommonResponse> AddProduct(AddProductRequest productValues, int userId)
        {
            using (var uow = this.unitOfWorkFactory.GetUnitOfWork())
            {
                var product = new Product
                {
                    ProductName = productValues.ProductName
                };

                uow.Repository <Product>().Add(product);
                var responseProduct = uow.SaveChanges();
                if (responseProduct.Response.status == false)
                {
                    return(CommonResponse.CreateError("Error occured while adding supplier products.", "ERR001", ""));
                }

                var supplierProduct = new SupplierProduct
                {
                    ProductId       = product.ProductId,
                    Quantity        = productValues.Quantity,
                    Composition     = productValues.Composition,
                    SupplierId      = userId,
                    AvailableStatus = "a"
                };
                uow.Repository <SupplierProduct>().Add(supplierProduct);

                var response = uow.SaveChanges();
                if (response.Response.status == false)
                {
                    return(CommonResponse.CreateError("Error occured while adding supplier products.", "ERR001", ""));
                }

                return(CommonResponse.CreateSuccess("Success", "SUC001", ""));
            }
        }
Пример #4
0
        public async Task <CommonResponse> SubmitAnalyzedSample(AnalyserSampleRequest values, int userId)
        {
            using (var uow = this.unitOfWorkFactory.GetUnitOfWork())
            {
                var productSupplierEntity = await uow.Repository <SupplierProduct>().Query().Select().SingleOrDefaultAsync(t => t.Id == values.ProductSupplierId);

                if (productSupplierEntity == null)
                {
                    return(CommonResponse.CreateError("Error", "Err02", "Product not found"));
                }

                var productAnalyzer = new ProductAnalyzer
                {
                    AnalyzerId          = userId,
                    SupplilerProductId  = values.ProductSupplierId ?? 1,
                    AnalyzedComposition = values.AnalysedComposition ?? 1,
                    IsDealPlaced        = "y"
                };

                uow.Repository <ProductAnalyzer>().Add(productAnalyzer);
                productSupplierEntity.Composition -= productAnalyzer.AnalyzedComposition;
                var response = uow.SaveChanges();
                if (response.Response.status == false)
                {
                    return(CommonResponse.CreateError("Error occured while adding analysing composition.", "ERR001", ""));
                }

                return(CommonResponse.CreateSuccess("Success", "SUC001", ""));
            }
        }
Пример #5
0
        public async Task <CommonResponse> DisableUser(int userId)
        {
            using (var uow = this.unitOfWorkFactory.GetUnitOfWork())
            {
                var user = await uow.Repository <User>().Query().Select().SingleOrDefaultAsync(t => t.Id == userId);

                if (user != null)
                {
                    return(CommonResponse.CreateError("Error", "Err02", "Uer not found"));
                }
                uow.Repository <User>().Delete(user);
                return(CommonResponse.CreateSuccess("Success", "Suc01", "User Removed"));
            }
        }
Пример #6
0
        public async Task <CommonResponse> Login(LoginRequest requestValues)
        {
            using (var uow = this.unitOfWorkFactory.GetUnitOfWork())
            {
                var tokenResponse = await this.tokenService.GenerateToken(requestValues.UserName, requestValues.Password);

                if (tokenResponse.ToLower().Contains("error"))
                {
                    return(CommonResponse.CreateError("Invalid user credentials, please try again.", "ERR001", ""));
                }
                dynamic newToken   = Newtonsoft.Json.JsonConvert.DeserializeObject(tokenResponse);
                string  tokenValue = "Bearer " + newToken.access_token;

                LoginResponse loginResponse = new LoginResponse();
                loginResponse.token        = tokenValue;
                loginResponse.UserFullName = newToken.FullName;
                return(CommonResponse.CreateSuccess("Logged in successfully.", "SUC001", loginResponse));
            }
        }
Пример #7
0
        public CommonResponse SaveChanges()
        {
            try
            {
                this.context.SaveChanges();
                return(CommonResponse.CreateSuccess("SaveChanges", "SUC001", ""));
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                return(CommonResponse.CreateError("SaveChanges", "ERR0001", ex.Message));
            }

            catch (DbUpdateConcurrencyException ex)
            {
                return(CommonResponse.CreateError("SaveChanges", "ERR0001", ex.Message));
            }

            catch (DbUpdateException ex)
            {
                return(CommonResponse.CreateError("SaveChanges", "ERR0001", ex.Message));
            }
            catch (Exception ex)
            {
                return(CommonResponse.CreateError("SaveChanges", "ERR0001", ex.Message));
            }
        }