public IActionResult Create([FromBody] ItemCreateEditModel itemModel) { string email = Request.Headers["Email"]; string password = Request.Headers["Password"]; ItemReturnModel itemReturnModel = new ItemReturnModel(); itemReturnModel.PicturesPath = new List <string>(); itemReturnModel.DocumentsPath = new List <string>(); //StatusResponse response = new StatusResponse(); //add pictures //itemModel.PictureBinary = itemModel.PictureBinary.Replace("data:image/png|tiff|jpg|gif;base64,", ""); //byte[] picture = Convert.FromBase64String(itemModel.PictureBinary); try { if (String.IsNullOrEmpty(email) || String.IsNullOrEmpty(password)) { return(BadRequest(_itemHelper.response(false, 401, "Email or Password is empty"))); } UserManager user = _userManagerService.AuthenticateUser(email, password); if (user == null) { return(BadRequest(_itemHelper.response(false, 401, "User can not authenticate please check email and password"))); } CommonEnum.UserRoleType roletype = (CommonEnum.UserRoleType)Enum.Parse(typeof(CommonEnum.UserRoleType), user.RoleManager.Name); if (roletype != CommonEnum.UserRoleType.admin) { return(BadRequest(_itemHelper.response(false, 401, "User not have access for create item"))); } //Generate QR code if (!String.IsNullOrEmpty(itemModel.Name)) { Guid fileName = Guid.NewGuid(); itemModel.QrCodePath = _itemHelper.QrProcess(itemModel.Name, Convert.ToString(fileName)); } //Assign model data to entity Items item = _itemHelper.ModelToEntity(itemModel, user.Id, new Items()); _itemsService.InsertItem(item); //Insert Pictures if (itemModel.Pictures.Count > 0) { _itemHelper.InsertPictures(item.Id, user.Id, itemModel, itemReturnModel); } //Insert Documents if (itemModel.Documents.Count > 0) { _itemHelper.InsertDocuments(item.Id, user.Id, itemModel, itemReturnModel); } itemReturnModel.Id = item.Id; itemReturnModel.QrCodePath = item.QrCodePath; itemReturnModel.Status = true; itemReturnModel.StatusCode = 201; return(Ok(itemReturnModel)); } catch (Exception ex) { return(BadRequest(_itemHelper.response(false, 504, "Internal server error"))); } }
public void InsertDocuments(int itemId, int userId, ItemCreateEditModel itemDataModel, ItemReturnModel itemDataReturnModel) { var documentDirectory = _configuration.GetConnectionString("DocumentDirectory"); foreach (var document in itemDataModel.Documents.Select(x => x.DocumentString).ToList()) { //var documentString = doc.Replace("data:image/png|tiff|jpg|gif;base64,", ""); Guid guid = Guid.NewGuid(); string fileName = Convert.ToString(guid); string path = "", documentType = "", extension = ""; var documentString = document.Split(',')[1]; documentType = document.Substring(0, document.IndexOf(";")); documentType = documentType.Replace("data:", ""); //Get Extension from file type extension = GetExtensionFromMimeType(documentType); fileName = Convert.ToString(guid) + extension; byte[] ducumentBytes = Convert.FromBase64String(documentString); //Store Image in azure Blob var _task = Task.Run(() => this.UploadFileToBlobAsync(fileName, ducumentBytes, documentType, documentDirectory)); _task.Wait(); path = _task.Result; //Store Image bytes in sql //byte[] documentBytes = Convert.FromBase64String(document); //path = Path.Combine(_hostingEnvironment.WebRootPath, "Documents", fileName); //System.IO.File.WriteAllBytesAsync(path, documentBytes); Documents documentEntity = new Documents() { ItemId = itemId, //Binary = documentBytes.ToArray(), Name = fileName, Path = path, CreatedBy = userId }; itemDataReturnModel.DocumentsPath.Add(path); _documentsService.InsertDocument(documentEntity); } }
public IActionResult Edit([FromBody] ItemCreateEditModel itemModel) { string email = Request.Headers["Email"]; string password = Request.Headers["Password"]; //ItemHelper itemHelper = new ItemHelper(_hostingEnvironment); ItemReturnModel itemReturnModel = new ItemReturnModel(); try { if (String.IsNullOrEmpty(email) || String.IsNullOrEmpty(password)) { return(BadRequest(_itemHelper.response(false, 500, "User can not authenticate please check email and password"))); } if (itemModel == null) { return(BadRequest(_itemHelper.response(false, 500, "Item model is empty"))); } UserManager user = _userManagerService.AuthenticateUser(email, password); if (user == null) { return(BadRequest(_itemHelper.response(false, 500, "User model is empty"))); } CommonEnum.UserRoleType roletype = (CommonEnum.UserRoleType)Enum.Parse(typeof(CommonEnum.UserRoleType), user.RoleManager.Name); if (itemModel.Id <= 0) { return(BadRequest(_itemHelper.response(false, 500, "Item id is not valid or empty"))); } if ((roletype != CommonEnum.UserRoleType.admin) && (roletype != CommonEnum.UserRoleType.management)) { return(BadRequest(_itemHelper.response(false, 500, "You dont have right's to update entries"))); } Items item = _itemsService.GetItemDataById(itemModel.Id); if (item != null && item.Name != itemModel.Name) { Guid fileName = Guid.NewGuid(); //Generate QR code itemModel.QrCodePath = _itemHelper.QrProcess(itemModel.Name, Convert.ToString(fileName)); } if (!String.IsNullOrEmpty(itemModel.Name)) { item = _itemHelper.ModelToEntity(itemModel, user.Id, item); item.Id = itemModel.Id; bool isSuccess = _itemsService.UpdateItem(item); if (isSuccess == false) { return(BadRequest(_itemHelper.response(false, 500, "Internal server error"))); } //Insert Pictures if (itemModel.Pictures.Count > 0) { try { _itemHelper.InsertPictures(item.Id, user.Id, itemModel, itemReturnModel); } catch (Exception ex) { throw; } } //Insert Documents if (itemModel.Documents.Count > 0) { _itemHelper.InsertDocuments(item.Id, user.Id, itemModel, itemReturnModel); } itemReturnModel.Id = itemModel.Id; itemReturnModel.QrCodePath = item.QrCodePath; itemReturnModel.Status = true; itemReturnModel.StatusCode = 200; return(Ok(itemReturnModel)); } return(BadRequest(_itemHelper.response(false, 500, "Internal server error"))); } catch (Exception ex) { return(BadRequest(_itemHelper.response(false, 500, ex.Message))); } }
public void InsertPictures(int itemId, int userId, ItemCreateEditModel itemModel, ItemReturnModel itemReturnModel) { var pictureDirectory = _configuration.GetConnectionString("PictureDirectory"); foreach (var picture in itemModel.Pictures.Select(x => x.PictureString).ToList()) { Guid guid = Guid.NewGuid(); string pictureFileName = "", path = "", pictureType = "", extension = ""; //var pictureString = picture.Replace("data:image/png|tiff|jpg|gif;base64,", ""); var pictureString = picture.Split(',')[1]; //pictureString = picture.ToString(); pictureType = picture.Substring(0, picture.IndexOf(";")); pictureType = pictureType.Replace("data:", ""); extension = GetExtensionFromMimeType(pictureType); pictureFileName = Convert.ToString(guid) + extension; //string converted = pictureString.Replace('-', '+'); //converted = converted.Replace('_', '/'); byte[] pictureBytes = Convert.FromBase64String(pictureString); //Store Image in azure Blob var _task = Task.Run(() => this.UploadFileToBlobAsync(pictureFileName, pictureBytes, pictureType, pictureDirectory)); _task.Wait(); path = _task.Result; //store image at location //string path = Path.Combine(_hostingEnvironment.WebRootPath, "Pictures", pictureFileName); //System.IO.File.WriteAllBytesAsync(path, pictureBytes); Pictures picturesEntity = new Pictures() { ItemId = itemId, //Binary = pictureBytes.ToArray(), Name = pictureFileName, Path = path, CreatedBy = userId }; itemReturnModel.PicturesPath.Add(path); _picturesService.InsertPictures(picturesEntity); } }