public async static Task <Product> CreateNew(Product newProduct, string lang)
        {
            if (newProduct == null || newProduct.Name == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            CulturalEnumStringConverter.Culture = new CultureInfo(lang);
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                if (newProduct.Graphic != null && newProduct.Graphic.Length > 4000)
                {
                    newProduct.Graphic = "";
                }

                List <Expression <Func <Model, object> > > rangesExpr = new List <Expression <Func <Model, object> > > {
                    m => m.Range,
                    m => m.Modules.Select(mod => mod.Components)
                };
                await ServiceHelper <Product> .LoadSingleNavigationProperty <Model>(newProduct, ctx, p => p.Model, _getCtxModels, rangesExpr, _setModel);

                ctx.Products.Add(newProduct);
                await ctx.SaveChangesAsync();

                return(newProduct);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create a new <see cref="ConceptionDevisWS.Models.Model"/> for an existing <see cref="ConceptionDevisWS.Models.Range"/>.
        /// </summary>
        /// <param name="rangeId">the range's identity</param>
        /// <param name="newModel">the model to store</param>
        /// <param name="lang">the culture to create the model into (fr-FR or en-US)</param>
        /// <returns>the stored model</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (when the given range does not exists or the model is null).</exception>
        public async static Task <Model> CreateNew(int rangeId, Model newModel, string lang)
        {
            if (newModel == null || newModel.Name == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            Range seekedRange = await _searchRange(rangeId, lang);

            newModel.Range = seekedRange;

            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);
            }

            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                List <Expression <Func <Range, object> > > modelsExpr = new List <Expression <Func <Range, object> > >();
                await ServiceHelper <Model> .LoadSingleNavigationProperty <Range>(newModel, ctx, m => m.Range, _getCtxRanges, modelsExpr, _setRange);

                ctx.Models.Add(newModel);
                await ctx.SaveChangesAsync();

                return(newModel);
            }
        }
        /// <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);
            }
        }
Beispiel #4
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);
            }
        }
        /// <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);
            }
        }
Beispiel #6
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);
            }
        }
        /// <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);
            }
        }
Beispiel #8
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();
            }
        }
        /// <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();
            }
        }
Beispiel #10
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();
            }
        }
Beispiel #11
0
        public async Task <IActionResult> Delete(string id)
        {
            User users = new User();

            users = _context.Users.Find(id);
            _context.Remove(users);
            await _context.SaveChangesAsync();

            return(View("Show", "ManageContent"));
        }
Beispiel #12
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();
            }
        }
Beispiel #13
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();
            }
        }
Beispiel #14
0
 /// <summary>
 /// Logs a <see cref="ConceptionDevisWS.Models.User"/> out.
 /// </summary>
 /// <param name="principal">the user's security identity</param>
 /// <remarks>
 /// This is not fully \htmlonly <accronym title="REpresentational State Transfer">REST</accronym>\endhtmlonly compliant, but it's usual.
 /// </remarks>
 public async static Task Logout(IPrincipal principal)
 {
     using (ModelsDBContext ctx = new ModelsDBContext())
     {
         RevokedToken revokedToken = new RevokedToken {
             Name = principal.Identity.Name
         };
         ctx.RevokedTokens.Add(revokedToken);
         await ctx.SaveChangesAsync();
     }
 }
        /// <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);
            }
        }
Beispiel #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();
                }
            }
        }
        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();
            }
        }
Beispiel #18
0
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicle)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                var productVehicle = _mapper.Map <VehicleResource, Vehicle>(vehicle);
                // _context.Vehicles.Add(productVehicle);
                await _context.SaveChangesAsync();

                var res = _mapper.Map <Vehicle, VehicleResource>(productVehicle);
                return(Ok(res));
            }
        }
Beispiel #19
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);
            }
        }
Beispiel #20
0
        /// <summary>
        /// Create an <see cref="ConceptionDevisWS.Models.User"/>.
        /// </summary>
        /// <param name="user">the user to store</param>
        /// <returns>the created user</returns>
        public async static Task <User> Register(User user)
        {
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                if (user == null || user.Login == null || user.Password == null)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                ctx.Users.Add(user);
                user.Rights   = ERights.ConceptionDevis;
                user.Password = HashManager.GetHash(user.Password);
                await ctx.SaveChangesAsync();

                user.Password = null;
                return(user);
            }
        }
Beispiel #21
0
        /// <summary>
        /// Create a new <see cref="ConceptionDevisWS.Models.Range"/>.
        /// </summary>
        /// <param name="newRange">the range to store</param>
        /// <param name="lang">the culture to create a range into (fr-FR or en-US)</param>
        /// <returns>the stored range</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (when the given range is null).</exception>
        public async static Task <Range> CreateNew(Range newRange, string lang)
        {
            if (newRange == null || newRange.Name == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            CulturalEnumStringConverter.Culture = new CultureInfo(lang);
            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                await ServiceHelper <Range> .InitNavigationProperty <Model>(newRange, ctx, _getModels, _getCtxModels);

                ctx.Ranges.Add(newRange);
                await ctx.SaveChangesAsync();

                return(newRange);
            }
        }
        public async Task <IActionResult> CreateContact([FromBody] ContactResource info)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                var contact = _mapper.Map <ContactResource, Contact>(info);

                _context.Contacts.Add(contact);
                await _context.SaveChangesAsync();

                var response = _mapper.Map <Contact, ContactResource>(contact);

                return(Ok(response));
            }
        }
        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);
            }
        }
Beispiel #24
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();
            }
        }
Beispiel #25
0
        /// <summary>
        /// Create a new <see cref="ConceptionDevisWS.Models.Client"/>.
        /// </summary>
        /// <param name="newClient">the client to store</param>
        /// <returns>the stored client</returns>
        /// <exception cref="HttpResponseException">In case something went wrong (when the given client is null).</exception>
        public async static Task <Client> CreateNew(Client newClient)
        {
            if (newClient == null || newClient.FirstName == null || newClient.LastName == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                await ServiceHelper <Client> .InitNavigationProperty <Project>(newClient, ctx, _getProjects, _getCtxProjects);

                List <Expression <Func <User, object> > > clientsExpr = new List <Expression <Func <User, object> > >();
                await ServiceHelper <Client> .LoadSingleNavigationProperty <User>(newClient, ctx, c => c.User, _getCtxUsers, clientsExpr, _setUser);

                ctx.Clients.Add(newClient);
                await ctx.SaveChangesAsync();

                return(newClient);
            }
        }
        /// <summary>
        /// Create a new <see cref="ConceptionDevisWS.Models.Project"/> for an existing <see cref="ConceptionDevisWS.Models.Client"/>.
        /// </summary>
        /// <param name="clientId">the client's identity</param>
        /// <param name="newProject">the project to store</param>
        /// <param name="lang">the culture to create the project into (fr-FR or en-US)</param>
        /// <returns>the created project</returns>
        public async static Task <Project> CreateNew(int clientId, Project newProject, string lang)
        {
            if (newProject == null || newProject.Name == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            await _searchClient(clientId, lang);

            newProject.Client.Id = clientId;

            using (ModelsDBContext ctx = new ModelsDBContext())
            {
                List <Expression <Func <Client, object> > > usersExpr = new List <Expression <Func <Client, object> > > ();
                await ServiceHelper <Project> .LoadSingleNavigationProperty <Client>(newProject, ctx, p => p.Client, _getCtxClients, usersExpr, _setClient);

                ctx.Projects.Add(newProject);
                await ctx.SaveChangesAsync();

                return(newProject);
            }
        }
        public async Task <IActionResult> AddArticles(Articles article)
        {
            Articles articles = new Articles();

            articles.ArticleName     = article.ArticleName;
            articles.ArticleSideName = article.ArticleSideName;

            var files    = Request.Form.Files;
            var fileName = files.First().FileName;
            var filePath = _hostingEnvironment.WebRootPath + "/articlepics/" + fileName;

            using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)){
                files.First().CopyTo(fileStream);
                fileStream.Flush();
            }
            string url         = Request.Host.Value;
            string requestPath = "https://" + url + ":8088/contents/" + fileName;

            if (article.Areas.Equals("1") || article.Areas.Equals("2") || article.Areas.Equals("3"))
            {
                articles.Areas = "首页" + article.Areas;
                if (article.Areas.Equals("2"))
                {
                    string selfpath = _hostingEnvironment.WebRootPath + "/contents/" + fileName;
                    var    image    = Image.Load(filePath);
                    image.Mutate(c => c.Resize(640, 426));
                    using (var selfstream = new FileStream(selfpath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        image.SaveAsPng(selfstream);
                    }
                }
                else
                {
                    requestPath = "https://" + url + ":8088/articlepics/" + fileName;
                }
            }
            else
            {
                articles.Areas = "关于我们" + article.Areas;
                if (article.Areas.Equals("5"))
                {
                    string selfpath = _hostingEnvironment.WebRootPath + "/contents/" + fileName;
                    var    image    = Image.Load(filePath);
                    image.Mutate(c => c.Resize(640, 426));
                    using (var selfstream = new FileStream(selfpath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        image.SaveAsPng(selfstream);
                    }
                }
                if (article.Areas.Equals("6"))
                {
                    string selfpath = _hostingEnvironment.WebRootPath + "/contents/" + fileName;
                    var    image    = Image.Load(filePath);
                    image.Mutate(c => c.Resize(200, 150));
                    using (var selfstream = new FileStream(selfpath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        image.SaveAsPng(selfstream);
                    }
                }
                else
                {
                    requestPath = "https://" + url + ":8088/articlepics/" + fileName;
                }
            }
            articles.ArticleImgUrl     = requestPath;
            articles.ArticleContext    = article.ArticleContext;
            articles.ArticleCreateDate = Convert.ToString(DateTime.Now);
            articles.ArticleUpdateDate = Convert.ToString(DateTime.Now);
            articles.UserName          = User.Identity.Name;

            Updates updates = new Updates();

            updates.UpdateContent = article.ArticleName;
            updates.UpdateDate    = Convert.ToString(DateTime.Now);
            if (article.Areas.Equals("1") || article.Areas.Equals("2") || article.Areas.Equals("3"))
            {
                updates.UpdateType = "首页" + article.Areas;
            }
            else
            {
                updates.UpdateType = "关于我们" + article.Areas;
            }
            updates.UpdateUserName = User.Identity.Name;
            try{
                _context.Add(articles);
                _context.Add(updates);
                await _context.SaveChangesAsync();

                return(View("Index"));
            }catch {}
            return(View("Error"));
        }