Ejemplo n.º 1
0
        public ResponseBase PatronIntroducerSet(PatronRequest request)
        {
            try
            {
                NullSafeCheck(request, "PatronRequest");
                NullSafeCheck(request.DTO, "DTO");

                if (request.DTO.ID <= 0)
                {
                    throw new CustomException(BAD_REQ_USER_MESSAGE, $"Patron ID {request.DTO.ID} is not valid");
                }
                if ((request.DTO.IntroducerID ?? 0) <= 0)
                {
                    throw new CustomException(BAD_REQ_USER_MESSAGE, $"IntroducerID {request.DTO.IntroducerID} is not valid");
                }

                using (var context = new CharityEntities())
                {
                    var patron = context.Patrons.FirstOrDefault(p => p.ID == request.DTO.ID);
                    patron.IntroducerID = request.DTO.IntroducerID;

                    context.SaveChanges();
                }

                return(new ResponseBase(true, 1));
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
                return(ExceptionToBaseResponse(ex));
            }
        }
Ejemplo n.º 2
0
        public PatronResponse PatronGetList(PatronRequest request)
        {
            List <PatronDTO> result = null;

            try
            {
                NullSafeCheck(request, "Request");
                NullSafeCheck(request.Filter, "Filter");

                using (var context = new CharityEntities())
                {
                    var filter = request.Filter;

                    var query = context.Patrons.AsNoTracking().AsQueryable();
                    if (filter.ID > 0)
                    {
                        query = query.Where(p => p.Active && p.ID == filter.ID);
                    }
                    else
                    {
                        if (filter.Active != null)
                        {
                            query = query.Where(p => p.Active == filter.Active);
                        }
                        if (filter.CreateDate?.From != null)
                        {
                            query = query.Where(p => p.CreateDate >= filter.CreateDate.From);
                        }
                        if (filter.CreateDate?.To != null)
                        {
                            query = query.Where(p => p.CreateDate <= filter.CreateDate.To);
                        }
                        if (filter.PersonID > 0)
                        {
                            query = query.Where(p => p.PersonID == filter.PersonID);
                        }
                    }
                    result = query.Select(PatronDTOMapper).ToList();

                    if (request.LoadPerson)
                    {
                        var persons = context.People.AsNoTracking();
                        result.ForEach(patron =>
                        {
                            var person    = persons.FirstOrDefault(p => p.ID == patron.PersonID);
                            patron.Person = PersonDTOMapper(person);
                        });
                    }
                }

                return(new PatronResponse {
                    Success = true, ResultList = result
                });
            }
            catch (Exception ex)
            {
                var response = ExceptionToBaseResponse(ex);
                return(response.Cast <PatronResponse>());
            }
        }
Ejemplo n.º 3
0
        public PatronResponse PatronGet(PatronRequest request)
        {
            try
            {
                NullSafeCheck(request, "Request");
                NullSafeCheck(request.Filter, "Filter");

                if (request.Filter.ID <= 0 && request.Filter.PersonID <= 0 && string.IsNullOrEmpty(request.Filter.NationalNo))
                {
                    throw new CustomException(INVALID_REQ_USER_MESSAGE, "ID,PersonID and NationalNo is invalid");
                }

                PatronDTO result = null;
                using (var context = new CharityEntities())
                {
                    var query = context.Patrons.AsNoTracking().AsQueryable();

                    if (request.Filter.ID > 0)
                    {
                        query = query.Where(p => p.Active && p.ID == request.Filter.ID);
                    }

                    else if (request.Filter.PersonID > 0)
                    {
                        query = query.Where(p => p.PersonID == request.Filter.PersonID);
                    }

                    else if (string.IsNullOrEmpty(request.Filter.NationalNo) == false)
                    {
                        var person = context.People.AsNoTracking().FirstOrDefault(p => p.NationalNo == request.Filter.NationalNo);
                        query = query.Where(p => p.PersonID == person.ID);
                    }

                    result = query.Select(PatronDTOMapper).FirstOrDefault();

                    if (request.LoadPerson)
                    {
                        var people = context.People.AsNoTracking();
                        var person = people.FirstOrDefault(p => p.ID == result.PersonID);
                        result.Person = PersonDTOMapper(person);
                    }
                }

                if (result == null)
                {
                    throw new CustomException("نتیجه ای یافت نشد", "Patron not found");
                }

                return(new PatronResponse {
                    Success = result != null, ResultID = result?.ID ?? 0, Result = result
                });
            }
            catch (Exception ex)
            {
                var response = ExceptionToBaseResponse(ex);
                return(response.Cast <PatronResponse>());
            }
        }
Ejemplo n.º 4
0
        public ResponseBase PatronSet(PatronRequest request)
        {
            try
            {
                NullSafeCheck(request, "PatronRequest");
                NullSafeCheck(request.DTO, "DTO");

                if (request.DTO.PersonID <= 0)
                {
                    throw new CustomException(BAD_REQ_USER_MESSAGE, $"PersonID {request.DTO.PersonID} is not valid");
                }

                var result = EntitySet <PatronDTO, Patron>(request.DTO);
                return(new ResponseBase(true, result));
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
                return(ExceptionToBaseResponse(ex));
            }
        }