// public string ImageToBase64(Image image, //System.Drawing.Imaging.ImageFormat format) // { // using (MemoryStream ms = new MemoryStream()) // { // // Convert Image to byte[] // image.Save(ms, format); // byte[] imageBytes = ms.ToArray(); // // Convert byte[] to Base64 String // string base64String = Convert.ToBase64String(imageBytes); // return base64String; // } // } //private string RezizeImage(Image img, int maxWidth, int maxHeight) //{ // if (img.Height <= maxHeight && img.Width <= maxWidth) return "1"; // using (img) // { // Double xRatio = (double)img.Width / maxWidth; // Double yRatio = (double)img.Height / maxHeight; // Double ratio = Math.Max(xRatio, yRatio); // int nnx = (int)Math.Floor(img.Width / ratio); // int nny = (int)Math.Floor(img.Height / ratio); // Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb); // using (Graphics gr = Graphics.FromImage(cpy)) // { // gr.Clear(Color.Transparent); // // This is said to give best quality when resizing images // gr.InterpolationMode = InterpolationMode.HighQualityBicubic; // gr.DrawImage(img, // new Rectangle(0, 0, nnx, nny), // new Rectangle(0, 0, img.Width, img.Height), // GraphicsUnit.Pixel); // } // return ImageToBase64(cpy,ImageFormat.Bmp); // } //} //private MemoryStream BytearrayToStream(byte[] arr) //{ // return new MemoryStream(arr, 0, arr.Length); //} //private String ResizeImageFile(string imageFile) //{ // int size = 640; // int quality = 65; // using (var image = new Bitmap(Base64ToImage(imageFile))) // { // int width, height; // if (image.Width > image.Height) // { // width = size; // height = Convert.ToInt32(image.Height * size / (double)image.Width); // } // else // { // width = Convert.ToInt32(image.Width * size / (double)image.Height); // height = size; // } // var resized = new Bitmap(width, height); // //Image result; // using (var graphics = Graphics.FromImage(resized)) // { // graphics.CompositingQuality = CompositingQuality.HighSpeed; // graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // graphics.CompositingMode = CompositingMode.SourceCopy; // graphics.DrawImage(image, 0, 0, width, height); // var qualityParamId = System.Drawing.Imaging.Encoder.Quality; // var encoderParameters = new EncoderParameters(1); // encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality); // var codec = ImageCodecInfo.GetImageDecoders() // .FirstOrDefault(codec => codec.FormatID == ImageFormat.Jpeg.Guid); // //resized.Save(result, codec, encoderParameters); // return ImageToBase64(resized, ImageFormat.Bmp); // } // } //} //private static Size CalculateDimensions(Size oldSize, int targetSize) //{ // Size newSize = new Size(); // if (oldSize.Height > oldSize.Width) // { // newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height)); // newSize.Height = targetSize; // } // else // { // newSize.Width = targetSize; // newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width)); // } // return newSize; //} private string CreateBodyWelcome(ModelUserCompany model) { string body = string.Empty; using (StreamReader reader = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), "TemplateEmail", "TemplateCreateCompany.html"))) { body = reader.ReadToEnd(); } body = body.Replace("{name}", model.Name); body = body.Replace("{namecomercial}", model.NameBussines); return(body); }
//[ApiExplorerSettings(IgnoreApi = true)] public async Task <IActionResult> CreateCompany([FromBody] ModelUserCompany model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.email, Name = model.Name, Email = model.email, Last_Name = model.Last_name }; Microsoft.AspNetCore.Identity.SignInResult resultUser = await _signInManager.PasswordSignInAsync(model.email, model.Password, isPersistent : false, lockoutOnFailure : false); if (resultUser.Succeeded) { if (context.UsersCompany.Count(x => x.IdUser == (context.Users.FirstOrDefault(y => y.Email == model.email).Id)) > 0) { return(BadRequest("El Usuario ya posee una Empresa asignada")); } else { await _userManager.AddToRoleAsync(context.Users.FirstOrDefault(x => x.Email == model.email), "Company"); if (context.Company.Count(x => x.Rut == model.Rut) > 0) { var UserCompany = new UsersCompany(); UserCompany.idCompany = model.Rut; UserCompany.IdUser = user.Id; context.UsersCompany.Add(UserCompany); context.SaveChanges(); //await _emailSender.SendEmailAsync(model.email, "Bienvenidos a la aplicación Aprovechapp!-.", CreateBodyWelcome(model)).ConfigureAwait(false); return(Ok()); } else { var company = new Company(); company.email = model.email; company.Name = model.Name; company.Rut = model.Rut; company.Name = model.NameBussines; context.Company.Add(company); context.SaveChanges(); var UserCompany = new UsersCompany(); UserCompany.idCompany = company.Rut; UserCompany.IdUser = user.Id; context.UsersCompany.Add(UserCompany); context.SaveChanges(); //await _emailSender.SendEmailAsync(model.email, "Bienvenidos a la aplicación Aprovechapp!-.", CreateBodyWelcome(model)).ConfigureAwait(false); return(Ok()); } } } else { var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(context.Users.FirstOrDefault(x => x.Email == model.email), "Company"); if (context.Company.Count(x => x.Rut == model.Rut) > 0) { var UserCompany = new UsersCompany(); UserCompany.idCompany = model.Rut; UserCompany.IdUser = user.Id; context.UsersCompany.Add(UserCompany); context.SaveChanges(); //await _emailSender.SendEmailAsync(model.email, "Bienvenidos a la aplicación Aprovechapp!-.", CreateBodyWelcome(model)).ConfigureAwait(false); return(Ok()); } else { var company = new Company(); company.email = model.email; company.Name = model.Name; company.Rut = model.Rut; company.Name = model.NameBussines; context.Company.Add(company); context.SaveChanges(); var UserCompany = new UsersCompany(); UserCompany.idCompany = company.Rut; UserCompany.IdUser = user.Id; context.UsersCompany.Add(UserCompany); context.SaveChanges(); //await _emailSender.SendEmailAsync(model.email, "Bienvenidos a la aplicación Aprovechapp!-.", CreateBodyWelcome(model)).ConfigureAwait(false); return(Ok()); } } else { return(BadRequest(result.Errors)); } } } else { return(BadRequest(ModelState)); } }