Exemplo n.º 1
0
        public async Task <RepositoryResult <IEnumerable <MakerInterfaceModel> > > SetMakersByUSerIdAsync(int userId, List <MakerInterfaceModel> setMakers)
        {
            try
            {
                if (userId <= 0)
                {
                    return(new RepositoryResult <IEnumerable <MakerInterfaceModel> >(HttpStatusCode.BadRequest));
                }
                var user = await AppUserManager.FindByIdAsync(userId);

                if (user == null)
                {
                    return(new RepositoryResult <IEnumerable <MakerInterfaceModel> >(HttpStatusCode.NotFound));
                }

                using (DataContext dbContext = DataContext.Create())
                {
                    dbContext.Database.ExecuteSqlCommand(RepositoryResource.IncrementResetUserMaker);

                    using (DbContextTransaction tx = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
                    {
                        var setMakerIds = setMakers.Select(m => m.Id).OrderBy(x => x).ToList();
                        var makerIds    = dbContext.MakerModels.Select(x => x.Id).OrderBy(x => x).ToList <int>();

                        IQueryable <UserMakerModel> userMakers = dbContext.UserMakerModels.Where(x => x.UserModelId == userId).OrderBy(x => x.MakerModelId);

                        foreach (int item in makerIds)
                        {
                            UserMakerModel check = userMakers.Where(x => x.MakerModelId == item).FirstOrDefault();
                            if (check == null && setMakerIds.Contains(item))
                            {
                                check              = new UserMakerModel();
                                check.UserModelId  = userId;
                                check.MakerModelId = item;
                                dbContext.UserMakerModels.Add(check);
                            }
                            else if (check != null && setMakerIds.Contains(item) && check.Deleted == true)
                            {
                                check.Deleted = false;
                                dbContext.Entry(check).State = EntityState.Modified;
                            }
                            else if (check != null && !setMakerIds.Contains(item) && check.Deleted == false)
                            {
                                check.Deleted = true;
                                dbContext.Entry(check).State = EntityState.Modified;
                            }
                        }
                        dbContext.SaveChanges();
                        tx.Commit();
                    }
                    dbContext.Database.ExecuteSqlCommand(RepositoryResource.IncrementResetUserMaker);

                    return(new RepositoryResult <IEnumerable <MakerInterfaceModel> >(HttpStatusCode.OK, setMakers));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public IHttpActionResult SetMakerList(int id, [FromBody] List <MakerApiModel> MakerList)
        {
            try
            {
                int count = 0;

                if (id <= 0 || UserManager.FindById(id) == null)
                {
                    return(BadRequest(Messages.ApiIllegalParameter));
                }

                dbContext.Database.ExecuteSqlCommand(ContextResources.IncrementResetUserMaker);

                using (DbContextTransaction tx =
                           dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
                {
                    List <int> setMakerList = MakerList.Select(m => m.Id).OrderBy(x => x).ToList();
                    IQueryable <UserMakerModel> userMakers = dbContext.UserMakerModels.Where(x => x.UserModelId == id).OrderBy(x => x.MakerModelId);
                    List <int> makerIdList = dbContext.MakerModels.Select(x => x.Id).OrderBy(x => x).ToList <int>();

                    foreach (int item in makerIdList)
                    {
                        UserMakerModel check = userMakers.Where(x => x.MakerModelId == item).FirstOrDefault();
                        if (check == null && setMakerList.Contains(item))
                        {
                            count++;
                            check              = new UserMakerModel();
                            check.UserModelId  = id;
                            check.MakerModelId = item;
                            dbContext.UserMakerModels.Add(check);
                        }
                        else if (check != null && setMakerList.Contains(item) && check.Deleted == true)
                        {
                            count++;
                            check.Deleted = false;
                            dbContext.Entry(check).State = EntityState.Modified;
                        }
                        else if (check != null && !setMakerList.Contains(item) && check.Deleted == false)
                        {
                            count++;
                            check.Deleted = true;
                            dbContext.Entry(check).State = EntityState.Modified;
                        }
                    }
#if false
                    if (dbContext.SaveChanges() == count)
                    {
                        tx.Rollback();
                        return(Conflict());
                    }
#endif
                    dbContext.SaveChanges();
                    tx.Commit();
                }
                dbContext.Database.ExecuteSqlCommand(ContextResources.IncrementResetUserMaker);

                return(Ok(MakerList));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }