Example #1
0
        /// <summary>
        /// Update completely the given <see cref="ConceptionDevisWS.Models.Range"/>.
        /// </summary>
        /// <param name="id">the range's identity</param>
        /// <param name="newRange">the updated range to store</param>
        /// <param name="lang">the culture to update this range into (fr-FR or en-US)</param>
        /// <returns>the updated range</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (for example, when the requested range does not exists).</exception>
        public async static Task <Range> UpdateRange(int id, Range newRange, string lang)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Range seekedRange = await GetRange(id, lang);

                ctx.Entry(seekedRange).State = EntityState.Modified;
                ctx.Entry(seekedRange).Collection(r => r.Models).EntityEntry.State = EntityState.Modified;

                await ServiceHelper <Range> .UpdateNavigationProperty <Model>(newRange, seekedRange, ctx, _getModels, _getCtxModels);

                seekedRange.UpdateNonComposedPropertiesFrom(newRange);
                bool updateSuccess = false;

                do
                {
                    try
                    {
                        await ctx.SaveChangesAsync();

                        updateSuccess = true;
                    }
                    catch (DbUpdateConcurrencyException dbuce)
                    {
                        DbEntityEntry entry = dbuce.Entries.Single();
                        entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync());
                    }
                } while (!updateSuccess);
                return(seekedRange);
            }
        }
Example #2
0
        /// <summary>
        /// Update completely the given <see cref="ConceptionDevisWS.Models.Client"/>.
        /// </summary>
        /// <param name="id">the client's identity</param>
        /// <param name="newClient">the updated client to store</param>
        /// <returns>the updated client</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (for example, when the requested client does not exists).</exception>
        public async static Task <Client> UpdateClient(int id, Client newClient)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                bool   updateSuccess = false;
                Client seekedClient  = await GetClient(id);

                ctx.Entry(seekedClient).State = EntityState.Modified;
                ctx.Entry(seekedClient).Collection(c => c.Projects).EntityEntry.State = EntityState.Modified;
                ctx.Entry(seekedClient).Reference(c => c.User).EntityEntry.State      = EntityState.Modified;


                await ServiceHelper <Client> .UpdateNavigationProperty <Project>(newClient, seekedClient, ctx, _getProjects, _getCtxProjects);


                seekedClient.UpdateNonComposedPropertiesFrom(newClient);
                do
                {
                    try
                    {
                        await ctx.SaveChangesAsync();

                        updateSuccess = true;
                    } catch (DbUpdateConcurrencyException dbuce)
                    {
                        DbEntityEntry entry = dbuce.Entries.Single();
                        entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync());
                    }
                } while (!updateSuccess);
                return(seekedClient);
            }
        }
Example #3
0
        /// <summary>
        /// Update a given module
        ///
        /// only components associations can be updated
        /// </summary>
        /// <param name="id">the module's identity</param>
        /// <param name="newModule">the updated module to store</param>
        /// <param name="lang">the culture to update the module into (fr-FR or en-US)</param>
        /// <returns>the updated module</returns>
        public async static Task <Module> UpdateModule(int id, Module newModule, string lang)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Module seekedModule = await GetModule(id, lang);

                ctx.Entry(seekedModule).State = EntityState.Modified;
                ctx.Entry(seekedModule).Collection(mod => mod.Components).EntityEntry.State = EntityState.Modified;

                await ServiceHelper <Module> .UpdateNavigationProperty <Component>(newModule, seekedModule, ctx, _getComposants, _getContextComponents);

                seekedModule.UpdateNonComposedPropertiesFrom(newModule);
                bool updateSuccess = false;
                do
                {
                    try
                    {
                        int affectedRows = await ctx.SaveChangesAsync();

                        updateSuccess = true;
                    } catch (DbUpdateConcurrencyException dbuce)
                    {
                        DbEntityEntry entry = dbuce.Entries.Single();
                        entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync());
                    }
                } while (!updateSuccess);

                return(seekedModule);
            }
        }
Example #4
0
        /// <summary>
        /// Update completely an <see cref="ConceptionDevisWS.Models.User"/>.
        /// </summary>
        /// <param name="id">the user's identity</param>
        /// <param name="newUser">the updated user</param>
        /// <param name="lang">the culture to update this user into (fr-FR or en-US)</param>
        /// <returns>the updated user</returns>
        /// <exception cref="HttpResponseException">In case something went wront (for example the requested user doesn't exist).</exception>
        public async static Task <User> UpdateUser(int id, User newUser, string lang)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                User seekedUser = await GetUser(id, lang);

                ctx.Entry(seekedUser).State = EntityState.Modified;
                ctx.Entry(seekedUser).Collection(u => u.Clients).EntityEntry.State = EntityState.Modified;

                await ServiceHelper <User> .UpdateNavigationProperty <Client>(newUser, seekedUser, ctx, _getClients, _getCtxClients);

                seekedUser.UpdateNonComposedPropertiesFrom(newUser);
                bool updateSuccess = false;
                do
                {
                    try
                    {
                        await ctx.SaveChangesAsync();

                        updateSuccess = true;
                    }
                    catch (DbUpdateConcurrencyException dbuce)
                    {
                        DbEntityEntry entry = dbuce.Entries.Single();
                        entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync());
                    }
                } while (!updateSuccess);
                return(seekedUser);
            }
        }
Example #5
0
        /// <summary>
        /// Remove the given <see cref="ConceptionDevisWS.Models.Range"/> and its <see cref="ConceptionDevisWS.Models.Model"/>s from storage.
        /// </summary>
        /// <param name="id">the range's identity</param>
        /// <exception cref="HttpResponseException">In case something went wrong (for example, when the requested range does not exists).</exception>
        public async static Task RemoveRange(int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Range seekedRange = await GetRange(id, "fr-FR");

                ctx.Entry(seekedRange).State = EntityState.Deleted;
                ctx.Entry(seekedRange).Collection(r => r.Models).EntityEntry.State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #6
0
        /// <summary>
        /// Update completely a given <see cref="ConceptionDevisWS.Models.Project"/>.
        /// </summary>
        /// <param name="clientId">the client's identity</param>
        /// <param name="id">the project's identity</param>
        /// <param name="newProject">the updated project </param>
        /// <param name="lang">the culture to update the project into (fr-FR or en-US)</param>
        /// <returns>the updated project</returns>
        /// <exception cref="HttpResponseException">In case either the client or project doesn't exist.</exception>
        public async static Task <Project> UpdateProject(int clientId, int id, Project newProject, string lang)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                if (newProject == null || newProject.Name == null)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                if (newProject.Client == null)
                {
                    newProject.Client = new Client();
                }
                newProject.Client.Id = clientId;

                Project seekedProject = await _searchClientProject(clientId, id, lang);

                ctx.Entry(seekedProject).State = EntityState.Modified;
                ctx.Entry(seekedProject).Collection(p => p.Products).EntityEntry.State = EntityState.Modified;
                List <Expression <Func <Client, object> > > clientInfosExpr = new List <Expression <Func <Client, object> > > {
                };
                await ServiceHelper <Project> .SetSingleNavigationProperty <Client>(newProject, seekedProject, ctx, p => p.Client, _getCtxClients, clientInfosExpr, _setClient);

                await ServiceHelper <Project> .UpdateNavigationProperty <Product>(newProject, seekedProject, ctx, _getProducts, _getCtxProducts);

                foreach (Product newProduct in newProject.Products)
                {
                    Product currentProduct = seekedProject.Products.Find(prod => prod.Id == newProduct.Id);
                    if (currentProduct != null)
                    {
                        currentProduct.UpdateNonComposableProperties(newProduct);
                    }
                }

                seekedProject.UpdateNonComposedPropertiesFrom(newProject);
                bool updateSuccess = false;
                do
                {
                    try
                    {
                        await ctx.SaveChangesAsync();

                        updateSuccess = true;
                    } catch (DbUpdateConcurrencyException dbuce)
                    {
                        DbEntityEntry entry = dbuce.Entries.Single();
                        entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync());
                    }
                } while (!updateSuccess);
                return(seekedProject);
            }
        }
Example #7
0
        public async static Task RemoveProduct(int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Product seekedProduct = await GetProduct(id, "fr-FR");

                ctx.Entry(seekedProduct).State = EntityState.Deleted;
                ctx.Entry(seekedProduct).Reference(p => p.Model).EntityEntry.State = EntityState.Unchanged;
                // deletes the relation between the removed product and its model
                ctx.Entry(seekedProduct).Collection(p => new Model[] { p.Model }).EntityEntry.State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #8
0
        /// <summary>
        /// Internal use only, Update completely a given <see cref="ConceptionDevisWS.Models.Project"/> Including its <see cref="ConceptionDevisWS.Models.Client"/>.
        /// </summary>
        /// <param name="originalClientId">the original client's identity</param>
        /// <param name="newClientId">the new client's identity</param>
        /// <param name="id">the project's identity</param>
        /// <param name="newProject">the updated project (possibly with a different client) </param>
        /// <param name="lang">the culture to udpate the project into (fr-FR or en-US)</param>
        /// <returns>the updated project</returns>
        /// <exception cref="HttpResponseException">In case either the client or project doesn't exist.</exception>
        public async static Task <Project> SpecialUpdateProject(int originalClientId, int newClientId, int id, Project newProject, string lang)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                if (newProject.Client == null)
                {
                    newProject.Client = new Client();
                }
                newProject.Client.Id = newClientId;

                if (newProject == null || newProject.Name == null)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                Project seekedProject = await _searchClientProject(originalClientId, id, lang);

                ctx.Entry(seekedProject).State = EntityState.Modified;
                List <Expression <Func <Client, object> > > usersExpr = new List <Expression <Func <Client, object> > > ();
                await ServiceHelper <Project> .SetSingleNavigationProperty <Client>(newProject, seekedProject, ctx, p => p.Client, _getCtxClients, usersExpr, _setClient);

                seekedProject.UpdateNonComposedPropertiesFrom(newProject);
                await ctx.SaveChangesAsync();

                return(seekedProject);
            }
        }
Example #9
0
        /// <summary>
        /// Update completely the given <see cref="ConceptionDevisWS.Models.Model"/>.
        /// </summary>
        /// <param name="rangeId">the range's identity</param>
        /// <param name="id">the model's identity</param>
        /// <param name="newModel">the updated model to store</param>
        /// <param name="lang">the culture to update this range into (fr-FR or en-US)</param>
        /// <returns>the updated model</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (for example, when the requested range or model does not exists).</exception>
        public async static Task <Model> UpdateModel(int rangeId, int id, Model newModel, string lang)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                if (newModel.Range == null)
                {
                    newModel.Range = new Range();
                }
                newModel.Range.Id = rangeId;

                if (!await Validate(rangeId, newModel))
                {
                    string errMsg = "Invalid content : The model ExternalFinishing or FrameQuality does not match its Range.";
                    HttpResponseMessage responseMessage = new HttpResponseMessage {
                        StatusCode = HttpStatusCode.BadRequest, Content = new StringContent(errMsg)
                    };
                    throw new HttpResponseException(responseMessage);
                }

                Model seekedModel = await _searchRangeModel(rangeId, id, lang);

                ctx.Entry(seekedModel).State = EntityState.Modified;
                ctx.Entry(seekedModel).Collection(m => m.Modules).EntityEntry.State = EntityState.Modified;
                List <Expression <Func <Range, object> > > modelsExpr = new List <Expression <Func <Range, object> > >();
                await ServiceHelper <Model> .SetSingleNavigationProperty <Range>(newModel, seekedModel, ctx, m => m.Range, _getCtxRanges, modelsExpr, _setRange);

                await ServiceHelper <Model> .UpdateNavigationProperty <Module>(newModel, seekedModel, ctx, _getModules, _getCtxModules);

                seekedModel.UpdateNonComposedPropertiesFrom(newModel);

                bool updateSuccess = true;
                do
                {
                    try
                    {
                        await ctx.SaveChangesAsync();

                        updateSuccess = true;
                    } catch (DbUpdateConcurrencyException dbuce)
                    {
                        DbEntityEntry entry = dbuce.Entries.Single();
                        entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync());
                    }
                } while (!updateSuccess);
                return(seekedModel);
            }
        }
Example #10
0
        public async static Task <Product> UpdateProduct(int id, Product newProduct, string lang)
        {
            if (newProduct == null || newProduct.Name == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Product seekedProduct = await GetProduct(id, lang);

                ctx.Entry(seekedProduct).State = EntityState.Modified;
                ctx.Entry(seekedProduct).Reference(p => p.Model).EntityEntry.State = EntityState.Unchanged;
                seekedProduct.UpdateNonComposableProperties(newProduct);
                await ctx.SaveChangesAsync();

                return(newProduct);
            }
        }
Example #11
0
        /// <summary>
        /// Remove the given <see cref="ConceptionDevisWS.Models.Client"/> from storage.
        /// </summary>
        /// <param name="id">the client's identity</param>
        /// <returns>The request's HttpStatusCode</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (for example, when the requested client does not exists).</exception>
        public async static Task RemoveClient(int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Client seekedClient = await GetClient(id);

                Client maderaClient = await ctx.Clients.FindAsync(1);

                IEnumerable <Task> tasks = seekedClient.Projects.Select(p => {
                    return(ProjectService.SpecialUpdateProject(p.Client.Id, maderaClient.Id, p.Id, p, "fr-FR"));
                });
                await Task.WhenAll(tasks);

                ctx.Entry(seekedClient).Collection(c => c.Projects).EntityEntry.State = EntityState.Modified;
                seekedClient.Projects.Clear();
                ctx.Entry(seekedClient).State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #12
0
        /// <summary>
        /// Remove the given module from storage.
        /// </summary>
        /// <param name="id">the module's identity</param>
        /// <exception cref="HttpResponseException">In case something went wront (for example, the given module is not found).</exception>
        public async static Task RemoveModule(int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Module module = await GetModule(id, "fr-FR");

                ctx.Entry(module).State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #13
0
        /// <summary>
        /// Remove the given <see cref="ConceptionDevisWS.Models.Model"/> from storage.
        /// </summary>
        /// <param name="rangeId">the range'es identity</param>
        /// <param name="id">the model's identity</param>
        /// <exception cref="HttpResponseException">In case something went wrong (for example, when the requested range or model does not exists).</exception>
        public async static Task RemoveModel(int rangeId, int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Model seekedModel = await _searchRangeModel(rangeId, id, "fr-FR");

                ctx.Entry(seekedModel).State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #14
0
        /// <summary>
        /// Remove the given <see cref="ConceptionDevisWS.Models.Project"/> from storage.
        /// </summary>
        /// <param name="clientId">the client's identity</param>
        /// <param name="id">the project's identity</param>
        /// <exception cref="HttpResponseException">In case either the client or project doesn't exist.</exception>
        public async static Task RemoveProject(int clientId, int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                Project seekedProject = await _searchClientProject(clientId, id, "fr-FR");

                ctx.Entry(seekedProject).State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #15
0
        /// <summary>
        /// Remove an <see cref="ConceptionDevisWS.Models.User"/>.
        /// </summary>
        /// <param name="id">the user's identity</param>
        /// <exception cref="HttpResponseException">In case something went wront (for example the requested user doesn't exist).</exception>
        public async static Task RemoveUser(int id)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                User seekedUser = await GetUser(id, "fr-FR");

                ctx.Entry(seekedUser).State = EntityState.Deleted;
                await ctx.SaveChangesAsync();
            }
        }
Example #16
0
        private async static Task _removeLogout(string login)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                RevokedToken seekedToken = await _findToken(login);

                if (seekedToken != null)
                {
                    ctx.Entry(seekedToken).State = EntityState.Deleted;
                    await ctx.SaveChangesAsync();
                }
            }
        }