Beispiel #1
0
        public DataResponse <EntityDepartments> Update(EntityDepartments entity)
        {
            var response = new DataResponse <EntityDepartments>();

            try
            {
                base.DBInit();

                #region Prepare model
                var model = DBEntity.Departments.FirstOrDefault(a => a.Id == entity.Id);
                model.DepartmentName = entity.DepartmentName;
                model.Description    = entity.Description;
                model.IsActive       = entity.IsActive;
                model.UpdatedOn      = entity.UpdatedOn;
                model.UpdatedBy      = entity.UpdatedBy;
                #endregion

                if (base.DBSaveUpdate(model) > 0)
                {
                    IEnumerable <DepartmentPrivilege> departmentPrivilege = DBEntity.DepartmentPrivileges.Where(a => a.DepartmentId == model.Id).ToList();
                    if (departmentPrivilege.Count() > 0)
                    {
                        DBEntity.DepartmentPrivileges.RemoveRange(departmentPrivilege);
                        DBEntity.SaveChanges();
                    }
                    if (entity.DepartmentPrivilegeIds != null && entity.DepartmentPrivilegeIds.Count() > 0)
                    {
                        foreach (var item in entity.DepartmentPrivilegeIds)
                        {
                            DBEntity.DepartmentPrivileges.Add(new DepartmentPrivilege
                            {
                                DepartmentId = model.Id,
                                PrivilegeId  = item,
                                CreatedBy    = model.CreatedBy,
                                CreatedOn    = model.CreatedOn,
                                UpdatedBy    = entity.UpdatedBy,
                                UpdatedOn    = entity.UpdatedOn
                            });
                            DBEntity.SaveChanges();
                        }
                    }
                    return(GetDepartmentById(model.Id));
                }
                else
                {
                    response.CreateResponse(DataResponseStatus.InternalServerError);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            finally
            {
                base.DBClose();
            }
            return(response);
        }
        public IHttpActionResult CreateDepartments(DepartmentModels model)
        {
            var response = new DataResponse <EntityDepartments>();

            var entitydepartments = new EntityDepartments();

            entitydepartments.DepartmentName      = model.DepartmentName;
            entitydepartments.Description         = model.Description;
            entitydepartments.BusinessId          = CurrentBusinessId;
            entitydepartments.CreatedBy           = CurrentUserId;
            entitydepartments.CreatedOn           = DateTime.UtcNow;
            entitydepartments.DepartmentPrivilege = model.DepartmentPrivilege;
            response = repository.Insert(entitydepartments);
            return(Ok <DataResponse>(response));
        }
Beispiel #3
0
        public DataResponse <EntityDepartments> Insert(EntityDepartments entity)
        {
            var response = new DataResponse <EntityDepartments>();

            try
            {
                base.DBInit();

                var model = new Database.Department
                {
                    DepartmentName = entity.DepartmentName,
                    Description    = entity.Description,
                    BusinessId     = entity.BusinessId,
                    StatusId       = 1,
                    IsActive       = true,
                    CreatedOn      = entity.CreatedOn,
                    CreatedBy      = entity.CreatedBy
                };
                if (base.DBSave(model) > 0)
                {
                    if (entity.DepartmentPrivilegeIds != null && entity.DepartmentPrivilegeIds.Count() > 0)
                    {
                        foreach (var item in entity.DepartmentPrivilegeIds)
                        {
                            DBEntity.DepartmentPrivileges.Add(new DepartmentPrivilege {
                                DepartmentId = model.Id, PrivilegeId = item, CreatedBy = entity.CreatedBy, CreatedOn = entity.CreatedOn
                            });
                            DBEntity.SaveChanges();
                        }
                    }
                    entity.Id = model.Id;
                    response.CreateResponse(entity, DataResponseStatus.OK);
                }
                else
                {
                    response.CreateResponse(DataResponseStatus.InternalServerError);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            finally
            {
                base.DBClose();
            }
            return(response);
        }
        public IHttpActionResult UpdateDepartment(DepartmentModels model)
        {
            var response = new DataResponse <EntityDepartments>();

            if (ModelState.IsValid)
            {
                var entityDepartments = new EntityDepartments();
                entityDepartments.Id                  = model.Id;
                entityDepartments.DepartmentName      = model.DepartmentName;
                entityDepartments.Description         = model.Description;
                entityDepartments.IsActive            = model.IsActive;
                entityDepartments.UpdatedBy           = CurrentUserId;
                entityDepartments.UpdatedOn           = System.DateTime.UtcNow;
                entityDepartments.DepartmentPrivilege = model.DepartmentPrivilege;
                response = repository.Update(entityDepartments);
            }
            return(Ok <DataResponse>(response));
        }
Beispiel #5
0
        public IHttpActionResult InsertDepartmentData(VMDepartment model)
        {
            var response = new DataResponse <EntityDepartments>();

            if (ModelState.IsValid)
            {
                var entityDepartment = new EntityDepartments
                {
                    Id                     = model.Id,
                    DepartmentName         = model.DepartmentName,
                    Description            = model.Description,
                    DepartmentPrivilegeIds = model.DepartmentPrivilegeIds,
                    IsActive               = model.IsActive,
                };
                entityDepartment.UpdatedBy  = entityDepartment.CreatedBy = CurrentUserId;
                entityDepartment.BusinessId = CurrentBusinessId;
                if (model.Id > 0)
                {
                    response = new RepositoryDepartments().Update(entityDepartment);
                }
                else
                {
                    response = new RepositoryDepartments().Insert(entityDepartment);
                }
                return(Ok <DataResponse>(response));
            }
            else
            {
                var errorList = ModelState.Where(a => a.Value.Errors.Any()).Select(s => new
                {
                    Key     = s.Key.Split('.').Last(),
                    Message = s.Value.Errors[0].ErrorMessage
                });
                return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = errorList }));
            }
        }