示例#1
0
        public async Task <IActionResult> AddProjectSector([FromBody] ProjectSectorModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string userIdVal = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            int    userId    = 0;

            if (!string.IsNullOrEmpty(userIdVal))
            {
                userId = Convert.ToInt32(userIdVal);
            }
            if (userId == 0)
            {
                return(BadRequest("Unauthorized user access to api"));
            }
            var response = await projectService.AddProjectSector(model, userId);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }
            return(Ok(true));
        }
示例#2
0
        public async Task <ApiResponse> AddEditProjectSector(ProjectSectorModel model, string UserId)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                var existRecord = await _dbContext.ProjectSector.FirstOrDefaultAsync(x => x.IsDeleted == false && x.ProjectId == model.ProjectId);

                if (existRecord == null)
                {
                    ProjectSector obj = new ProjectSector();
                    obj.ProjectId   = model.ProjectId;
                    obj.SectorId    = model.SectorId;
                    obj.IsDeleted   = false;
                    obj.CreatedById = UserId;
                    obj.CreatedDate = DateTime.UtcNow;
                    await _dbContext.ProjectSector.AddAsync(obj);

                    await _dbContext.SaveChangesAsync();
                }
                else
                {
                    if (existRecord != null)
                    {
                        existRecord.ProjectId    = model.ProjectId;
                        existRecord.SectorId     = model.SectorId;
                        existRecord.IsDeleted    = false;
                        existRecord.ModifiedById = UserId;
                        existRecord.ModifiedDate = DateTime.UtcNow;
                        _dbContext.ProjectSector.Update(existRecord);
                        await _dbContext.SaveChangesAsync();
                    }
                }
                response.StatusCode = StaticResource.successStatusCode;
                response.Message    = "Success";
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(AddSectorDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response     = new ApiResponse();
            long        LatestCodeId = 0;

            if (request != null && !string.IsNullOrWhiteSpace(request.SectorName))
            {
                var code = string.Empty;

                try
                {
                    var data = _dbContext.SectorDetails.FirstOrDefault(x => x.IsDeleted == false && x.SectorName.Trim().ToLower() == request.SectorName.Trim().ToLower());

                    if (data == null)
                    {
                        SectorDetails obj          = new SectorDetails();
                        var           sectorDetail = _dbContext.SectorDetails
                                                     .OrderByDescending(x => x.SectorId)
                                                     .FirstOrDefault(x => x.IsDeleted == false);
                        if (sectorDetail == null)
                        {
                            LatestCodeId = 1;
                            code         = ProjectUtility.GenerateCode(LatestCodeId);
                        }
                        else
                        {
                            LatestCodeId = sectorDetail.SectorId + 1;
                            code         = ProjectUtility.GenerateCode(LatestCodeId);
                        }
                        obj.SectorName  = request.SectorName;
                        obj.IsDeleted   = false;
                        obj.SectorCode  = code;
                        obj.CreatedById = request.CreatedById;
                        obj.CreatedDate = DateTime.UtcNow;
                        await _dbContext.SectorDetails.AddAsync(obj);

                        await _dbContext.SaveChangesAsync();

                        if (obj.SectorId != 0)
                        {
                            ProjectSectorModel projectSectorModel = new ProjectSectorModel()
                            {
                                SectorId        = obj.SectorId,
                                ProjectId       = request.ProjectId,
                                ProjectSectorId = 0
                            };


                            var addEditProjectSector = await _iProjectServices.AddEditProjectSector(projectSectorModel, request.CreatedById);

                            if (addEditProjectSector.StatusCode == 200)
                            {
                                response.StatusCode         = StaticResource.successStatusCode;
                                response.data.SectorDetails = obj;
                                response.Message            = "Success";
                            }
                            else
                            {
                                throw new Exception("Project Sector could not be saved");
                            }
                        }
                    }
                    else
                    {
                        response.StatusCode = StaticResource.NameAlreadyExist;
                        response.Message    = StaticResource.ListNameAlreadyExist;
                    }
                }
                catch (Exception ex)
                {
                    response.StatusCode = StaticResource.failStatusCode;
                    response.Message    = StaticResource.SomethingWrong + ex.Message;
                }
            }
            else if (request != null && string.IsNullOrWhiteSpace(request.SectorName)) //check for emptystring
            {
                response.StatusCode = StaticResource.notValid;
                response.Message    = StaticResource.validData;
            }
            else if (request == null)
            {
                response.StatusCode = StaticResource.NameAlreadyExist;
            }
            return(response);
        }